From 95eb1dec91c6db56856a3700f8efb4d5561778d8 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Tue, 17 Oct 2023 21:44:53 -0700 Subject: [PATCH] introduce logic for shared native module queue Summary: Changelog: [Internal] currently, each native module creates a new module queue if `methodQueue` is not overridden in the native module. we want to see if we can use a single execution queue for a few reasons: - parity with android's queue model - performance: creating so many queues... for what? the overhead of this feels like it exceeds any potential benefit - set us up to remove the assocs from the module to the method queue, which will allow us to deprecate `synthesize methodQueue` and `-(dispatch_queue_t)moduleQueue` API. in this QE, we just start with replacing the KVO assoc'd queue with the shared module queue. Differential Revision: D50398635 --- .../ios/ReactCommon/RCTTurboModuleManager.mm | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm index b427ca018030ce..3edb86d24deea5 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm @@ -206,6 +206,9 @@ @implementation RCTTurboModuleManager { RCTBridgeProxy *_bridgeProxy; RCTBridgeModuleDecorator *_bridgeModuleDecorator; + + BOOL _enableSharedModuleQueue; + dispatch_queue_t _sharedModuleQueue; } - (instancetype)initWithBridge:(RCTBridge *)bridge @@ -221,6 +224,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge _bridgeProxy = bridgeProxy; _bridgeModuleDecorator = bridgeModuleDecorator; _invalidating = false; + _enableSharedModuleQueue = NO; if (RCTTurboModuleInteropEnabled()) { NSMutableDictionary> *legacyInitializedModules = [NSMutableDictionary new]; @@ -685,8 +689,12 @@ - (BOOL)_shouldCreateObjCModule:(Class)moduleClass * following if condition's block. */ if (!methodQueue) { - NSString *methodQueueName = [NSString stringWithFormat:@"com.facebook.react.%sQueue", moduleName]; - methodQueue = dispatch_queue_create(methodQueueName.UTF8String, DISPATCH_QUEUE_SERIAL); + if (_enableSharedModuleQueue) { + methodQueue = [self _sharedModuleQueue]; + } else { + NSString *methodQueueName = [NSString stringWithFormat:@"com.facebook.react.%sQueue", moduleName]; + methodQueue = dispatch_queue_create(methodQueueName.UTF8String, DISPATCH_QUEUE_SERIAL); + } if (moduleHasMethodQueueGetter) { /** @@ -1067,4 +1075,12 @@ - (void)_invalidateModules _legacyModuleCache.clear(); } +- (dispatch_queue_t)_sharedModuleQueue +{ + if (!_sharedModuleQueue) { + _sharedModuleQueue = dispatch_queue_create("com.meta.react.turbomodulemanager.queue", DISPATCH_QUEUE_SERIAL); + } + return _sharedModuleQueue; +} + @end