Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit d237f73

Browse files
committed
Introduce TurboModule Setup Metric (facebook#24732)
Summary: With the introduction of TurboModules, it would be beneficial to measure the setup time of these modules, as we currently have it in place for NativeModules. The instantiation of the TMs occurs in the `RCTTurboModuleManager`. In order to successfully measure the time it took to setup the module, we need to ensure that we don't take into account cached modules. As such, we need to: 1. Check if module is in `_turboModuleCache` a. Start mark for `RCTPLTurboModuleSetup` tag if not found 2. Get the TM via `[self provideTurboModule:]` 3. Check if module is in `_turboModuleCache` a. Stop mark for `RCTPLTurboModuleSetup` if we did not find module in cache prior to **step 2** and if it's now present in the cache. b. Notify about setup time if the above is true. 4. Return TM [iOS] [Added] - Gain insights on the the turbo module setup times by observing `RCTDidSetupModuleNotification`. The userInfo dictionary will contain the module name and setup time in milliseconds. These values can be extracted via `RCTDidSetupModuleNotificationModuleNameKey` and `RCTDidSetupModuleNotificationSetupTimeKey`. Pull Request resolved: facebook#24732 Differential Revision: D15362088 Pulled By: RSNara fbshipit-source-id: e6a8044e4aba5a12ae63e9c7dbf707a17ec00180
1 parent 639392d commit d237f73

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

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: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#import <cassert>
1111

12+
#import <React/RCTBridge+Private.h>
13+
#import <React/RCTPerformanceLogger.h>
1214
#import <React/RCTBridgeModule.h>
1315
#import <React/RCTCxxModule.h>
1416
#import <React/RCTLog.h>
@@ -66,19 +68,45 @@ - (instancetype)initWithRuntime:(jsi::Runtime *)runtime
6668

6769
__strong __typeof(self) strongSelf = weakSelf;
6870

71+
auto moduleName = name.c_str();
72+
auto moduleWasNotInitialized = ![strongSelf moduleIsInitialized:moduleName];
73+
if (moduleWasNotInitialized) {
74+
[strongSelf->_bridge.performanceLogger markStartForTag:RCTPLTurboModuleSetup];
75+
}
76+
6977
/**
7078
* By default, all TurboModules are long-lived.
7179
* Additionally, if a TurboModule with the name `name` isn't found, then we
7280
* trigger an assertion failure.
7381
*/
74-
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;
7590
};
7691

7792
_binding = std::make_shared<react::TurboModuleBinding>(moduleProvider);
7893
}
7994
return self;
8095
}
8196

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+
82110
/**
83111
* Given a name for a TurboModule, return a C++ object which is the instance
84112
* of that TurboModule C++ class. This class wraps the TurboModule's ObjC instance.

0 commit comments

Comments
 (0)