Skip to content

Commit bc95edb

Browse files
committed
Add TurboModule Setup Metrics
1 parent 2becfab commit bc95edb

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

React/Base/RCTBridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ RCT_EXTERN NSString *const RCTJavaScriptDidFailToLoadNotification;
4949
RCT_EXTERN NSString *const RCTDidInitializeModuleNotification;
5050

5151
/**
52-
* This notification fires each time a native module is setup after it is initialized. The
52+
* This notification fires each time a module is setup after it is initialized. The
5353
* `RCTDidSetupModuleNotificationModuleNameKey` key will contain a reference to the module name and
5454
* `RCTDidSetupModuleNotificationSetupTimeKey` will contain the setup time in ms.
5555
*/

React/Base/RCTPerformanceLogger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ typedef NS_ENUM(NSUInteger, RCTPLTag) {
2121
RCTPLNativeModulePrepareConfig,
2222
RCTPLNativeModuleMainThreadUsesCount,
2323
RCTPLNativeModuleSetup,
24+
RCTPLTurboModuleSetup,
2425
RCTPLJSCWrapperOpenLibrary,
2526
RCTPLBridgeStartup,
2627
RCTPLTTI,

React/Base/RCTPerformanceLogger.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ - (instancetype)init
4242
@"NativeModuleInjectConfig",
4343
@"NativeModuleMainThreadUsesCount",
4444
@"NativeModuleSetup",
45+
@"TurboModuleSetup",
4546
@"JSCWrapperOpenLibrary",
4647
@"JSCExecutorSetup",
4748
@"BridgeStartup",

ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <cassert>
1111

1212
#import <React/RCTBridge+Private.h>
13+
#import <React/RCTPerformanceLogger.h>
1314
#import <React/RCTBridgeModule.h>
1415
#import <React/RCTCxxModule.h>
1516
#import <React/RCTLog.h>
@@ -67,19 +68,45 @@ - (instancetype)initWithRuntime:(jsi::Runtime *)runtime
6768

6869
__strong __typeof(self) strongSelf = weakSelf;
6970

71+
auto moduleName = name.c_str();
72+
auto moduleWasNotInitialized = ![strongSelf moduleIsInitialized:moduleName];
73+
if (moduleWasNotInitialized) {
74+
[strongSelf->_bridge.performanceLogger markStartForTag:RCTPLTurboModuleSetup];
75+
}
76+
7077
/**
7178
* By default, all TurboModules are long-lived.
7279
* Additionally, if a TurboModule with the name `name` isn't found, then we
7380
* trigger an assertion failure.
7481
*/
75-
return [strongSelf provideTurboModule: name.c_str()];
82+
auto turboModule = [strongSelf provideTurboModule:moduleName];
83+
84+
if (moduleWasNotInitialized && [strongSelf moduleIsInitialized:moduleName]) {
85+
[strongSelf->_bridge.performanceLogger markStopForTag:RCTPLTurboModuleSetup];
86+
[strongSelf notifyAboutTurboModuleSetup:moduleName];
87+
}
88+
89+
return turboModule;
7690
};
7791

7892
_binding = std::make_shared<react::TurboModuleBinding>(moduleProvider);
7993
}
8094
return self;
8195
}
8296

97+
- (void)notifyAboutTurboModuleSetup:(const char*)name {
98+
NSString *moduleName = [[NSString alloc] initWithUTF8String:name];
99+
if (moduleName) {
100+
int64_t setupTime = [self->_bridge.performanceLogger durationForTag:RCTPLTurboModuleSetup];
101+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTDidSetupModuleNotification
102+
object:nil
103+
userInfo:@{
104+
RCTDidSetupModuleNotificationModuleNameKey: moduleName,
105+
RCTDidSetupModuleNotificationSetupTimeKey: @(setupTime)
106+
}];
107+
}
108+
}
109+
83110
/**
84111
* Given a name for a TurboModule, return a C++ object which is the instance
85112
* of that TurboModule C++ class. This class wraps the TurboModule's ObjC instance.

0 commit comments

Comments
 (0)