Skip to content

Commit ea96a7e

Browse files
tadeuzagallofacebook-github-bot-0
authored andcommitted
Avoid dispatch_async on RCTProfile when not profiling
Summary: public Fixes facebook#3953 Bail out soon when the profiler is not running + move string formating into the macro so that it happens in a background queue. Reviewed By: jspahrsummers Differential Revision: D2696167 fb-gh-sync-id: a1b91ee4459078ab9a4c0be62bd23362ec05e208
1 parent 9365414 commit ea96a7e

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

React/Base/RCTBatchedBridge.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,12 +871,11 @@ - (void)_jsThreadUpdate:(CADisplayLink *)displayLink
871871
for (RCTModuleData *moduleData in _frameUpdateObservers) {
872872
id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)moduleData.instance;
873873
if (!observer.paused) {
874-
RCT_IF_DEV(NSString *name = [NSString stringWithFormat:@"[%@ didUpdateFrame:%f]", observer, displayLink.timestamp];)
875874
RCTProfileBeginFlowEvent();
876875

877876
[self dispatchBlock:^{
878877
RCTProfileEndFlowEvent();
879-
RCT_PROFILE_BEGIN_EVENT(0, name, nil);
878+
RCT_PROFILE_BEGIN_EVENT(0, [NSString stringWithFormat:@"[%@ didUpdateFrame:%f]", observer, displayLink.timestamp], nil);
880879
[observer didUpdateFrame:frameUpdate];
881880
RCT_PROFILE_END_EVENT(0, @"objc_call,fps", nil);
882881
} queue:moduleData.methodQueue];

React/Profiler/RCTProfile.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,16 @@ RCT_EXTERN void _RCTProfileBeginEvent(NSThread *calleeThread,
6666
uint64_t tag,
6767
NSString *name,
6868
NSDictionary *args);
69-
#define RCT_PROFILE_BEGIN_EVENT(...) { \
70-
NSThread *calleeThread = [NSThread currentThread]; \
71-
NSTimeInterval time = CACurrentMediaTime(); \
72-
dispatch_async(RCTProfileGetQueue(), ^{ \
73-
_RCTProfileBeginEvent(calleeThread, time, __VA_ARGS__); \
74-
}); \
75-
}
69+
#define RCT_PROFILE_BEGIN_EVENT(...) \
70+
do { \
71+
if (RCTProfileIsProfiling()) { \
72+
NSThread *calleeThread = [NSThread currentThread]; \
73+
NSTimeInterval time = CACurrentMediaTime(); \
74+
dispatch_async(RCTProfileGetQueue(), ^{ \
75+
_RCTProfileBeginEvent(calleeThread, time, __VA_ARGS__); \
76+
}); \
77+
} \
78+
} while(0)
7679

7780
/**
7881
* The ID returned by BeginEvent should then be passed into EndEvent, with the
@@ -86,14 +89,17 @@ RCT_EXTERN void _RCTProfileEndEvent(NSThread *calleeThread,
8689
NSString *category,
8790
NSDictionary *args);
8891

89-
#define RCT_PROFILE_END_EVENT(...) { \
90-
NSThread *calleeThread = [NSThread currentThread]; \
91-
NSString *threadName = RCTCurrentThreadName(); \
92-
NSTimeInterval time = CACurrentMediaTime(); \
93-
dispatch_async(RCTProfileGetQueue(), ^{ \
94-
_RCTProfileEndEvent(calleeThread, threadName, time, __VA_ARGS__); \
95-
}); \
96-
}
92+
#define RCT_PROFILE_END_EVENT(...) \
93+
do { \
94+
if (RCTProfileIsProfiling()) { \
95+
NSThread *calleeThread = [NSThread currentThread]; \
96+
NSString *threadName = RCTCurrentThreadName(); \
97+
NSTimeInterval time = CACurrentMediaTime(); \
98+
dispatch_async(RCTProfileGetQueue(), ^{ \
99+
_RCTProfileEndEvent(calleeThread, threadName, time, __VA_ARGS__); \
100+
}); \
101+
} \
102+
} while(0)
97103

98104
/**
99105
* Collects the initial event information for the event and returns a reference ID

0 commit comments

Comments
 (0)