From 7a60c0bebe917da16bd15ee170a4204305d14ed5 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Fri, 20 Oct 2023 13:18:21 -0700 Subject: [PATCH] introduce logic for shared native module queue (#41042) 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. Reviewed By: cipolleschi Differential Revision: D50398635 --- .../ios/ReactCommon/RCTTurboModuleManager.mm | 16 ++++++++++++++-- 1 file changed, 14 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..5feb7b578dce74 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,11 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge _bridgeProxy = bridgeProxy; _bridgeModuleDecorator = bridgeModuleDecorator; _invalidating = false; + _enableSharedModuleQueue = NO; + + if (_enableSharedModuleQueue) { + _sharedModuleQueue = dispatch_queue_create("com.meta.react.turbomodulemanager.queue", DISPATCH_QUEUE_SERIAL); + } if (RCTTurboModuleInteropEnabled()) { NSMutableDictionary> *legacyInitializedModules = [NSMutableDictionary new]; @@ -685,8 +693,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 = _sharedModuleQueue; + } else { + NSString *methodQueueName = [NSString stringWithFormat:@"com.facebook.react.%sQueue", moduleName]; + methodQueue = dispatch_queue_create(methodQueueName.UTF8String, DISPATCH_QUEUE_SERIAL); + } if (moduleHasMethodQueueGetter) { /**