From ea96a7edb8a17fbcc57db99217783302bb790056 Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Thu, 26 Nov 2015 03:15:04 -0800 Subject: [PATCH] Avoid dispatch_async on RCTProfile when not profiling Summary: public Fixes #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 --- React/Base/RCTBatchedBridge.m | 3 +-- React/Profiler/RCTProfile.h | 36 ++++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/React/Base/RCTBatchedBridge.m b/React/Base/RCTBatchedBridge.m index 0430f2b0a..89861deda 100644 --- a/React/Base/RCTBatchedBridge.m +++ b/React/Base/RCTBatchedBridge.m @@ -871,12 +871,11 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR for (RCTModuleData *moduleData in _frameUpdateObservers) { id observer = (id)moduleData.instance; if (!observer.paused) { - RCT_IF_DEV(NSString *name = [NSString stringWithFormat:@"[%@ didUpdateFrame:%f]", observer, displayLink.timestamp];) RCTProfileBeginFlowEvent(); [self dispatchBlock:^{ RCTProfileEndFlowEvent(); - RCT_PROFILE_BEGIN_EVENT(0, name, nil); + RCT_PROFILE_BEGIN_EVENT(0, [NSString stringWithFormat:@"[%@ didUpdateFrame:%f]", observer, displayLink.timestamp], nil); [observer didUpdateFrame:frameUpdate]; RCT_PROFILE_END_EVENT(0, @"objc_call,fps", nil); } queue:moduleData.methodQueue]; diff --git a/React/Profiler/RCTProfile.h b/React/Profiler/RCTProfile.h index 5829a8eb4..2e6e921d3 100644 --- a/React/Profiler/RCTProfile.h +++ b/React/Profiler/RCTProfile.h @@ -66,13 +66,16 @@ RCT_EXTERN void _RCTProfileBeginEvent(NSThread *calleeThread, uint64_t tag, NSString *name, NSDictionary *args); -#define RCT_PROFILE_BEGIN_EVENT(...) { \ - NSThread *calleeThread = [NSThread currentThread]; \ - NSTimeInterval time = CACurrentMediaTime(); \ - dispatch_async(RCTProfileGetQueue(), ^{ \ - _RCTProfileBeginEvent(calleeThread, time, __VA_ARGS__); \ - }); \ -} +#define RCT_PROFILE_BEGIN_EVENT(...) \ + do { \ + if (RCTProfileIsProfiling()) { \ + NSThread *calleeThread = [NSThread currentThread]; \ + NSTimeInterval time = CACurrentMediaTime(); \ + dispatch_async(RCTProfileGetQueue(), ^{ \ + _RCTProfileBeginEvent(calleeThread, time, __VA_ARGS__); \ + }); \ + } \ + } while(0) /** * The ID returned by BeginEvent should then be passed into EndEvent, with the @@ -86,14 +89,17 @@ RCT_EXTERN void _RCTProfileEndEvent(NSThread *calleeThread, NSString *category, NSDictionary *args); -#define RCT_PROFILE_END_EVENT(...) { \ - NSThread *calleeThread = [NSThread currentThread]; \ - NSString *threadName = RCTCurrentThreadName(); \ - NSTimeInterval time = CACurrentMediaTime(); \ - dispatch_async(RCTProfileGetQueue(), ^{ \ - _RCTProfileEndEvent(calleeThread, threadName, time, __VA_ARGS__); \ - }); \ -} +#define RCT_PROFILE_END_EVENT(...) \ + do { \ + if (RCTProfileIsProfiling()) { \ + NSThread *calleeThread = [NSThread currentThread]; \ + NSString *threadName = RCTCurrentThreadName(); \ + NSTimeInterval time = CACurrentMediaTime(); \ + dispatch_async(RCTProfileGetQueue(), ^{ \ + _RCTProfileEndEvent(calleeThread, threadName, time, __VA_ARGS__); \ + }); \ + } \ + } while(0) /** * Collects the initial event information for the event and returns a reference ID