From fae798090dd88d068cb0e03c0ee451a5fa5b9e2d Mon Sep 17 00:00:00 2001 From: Iurii Khvorost Date: Tue, 16 Jul 2024 18:26:08 +0300 Subject: [PATCH] Registering native modules optimization --- Sources/ReactBridgeUtils/ReactBridgeUtils.m | 50 +++++++++++---------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/Sources/ReactBridgeUtils/ReactBridgeUtils.m b/Sources/ReactBridgeUtils/ReactBridgeUtils.m index 7ed90a6..b0f516a 100644 --- a/Sources/ReactBridgeUtils/ReactBridgeUtils.m +++ b/Sources/ReactBridgeUtils/ReactBridgeUtils.m @@ -35,33 +35,37 @@ BOOL isSync; } RCTMethodInfo; -@implementation ReactBridgeUtils - -+ (void)load { - SEL selector = @selector(_registerModule); - - int numClasses = objc_getClassList(NULL, 0); - Class* classes = (Class *)malloc(sizeof(Class) * numClasses); - numClasses = objc_getClassList(classes, numClasses); - - for (int i = 0; i < numClasses; i++) { - Class class = classes[i]; - - unsigned int numMethods = 0; - Method *methods = class_copyMethodList(object_getClass(class), &numMethods); - for (int j = 0; j < numMethods; j++) { - Method method = methods[j]; - if (sel_isEqual(method_getName(method), selector)) { - IMP imp = method_getImplementation(method); - ((void (*)(Class, SEL))imp)(class, selector); - continue; - } +static void class_performClassSelector(Class class, SEL selector) { + unsigned int numMethods = 0; + Method *methods = class_copyMethodList(object_getClass(class), &numMethods); + for (int i = 0; i < numMethods; i++) { + Method method = methods[i]; + if (sel_isEqual(method_getName(method), selector)) { + IMP imp = method_getImplementation(method); + ((void (*)(Class, SEL))imp)(class, selector); + break; } - free(methods); } - free(classes); + free(methods); } +__attribute__((constructor)) +static void load() { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + SEL selector = @selector(_registerModule); + int numClasses = objc_getClassList(NULL, 0); + Class* classes = (Class *)malloc(sizeof(Class) * numClasses); + numClasses = objc_getClassList(classes, numClasses); + for (int i = 0; i < numClasses; i++) { + Class class = classes[i]; + class_performClassSelector(class, selector); + } + free(classes); + }); +} + +@implementation ReactBridgeUtils + + (const void *)methodInfo:(NSString *)jsName objcName:(NSString *)objcName isSync:(BOOL)isSync { RCTMethodInfo* methodInfo = malloc(sizeof(RCTMethodInfo)); methodInfo->jsName = strdup([jsName cStringUsingEncoding: NSUTF8StringEncoding]);