Only create TurboModules, if they're registered (#37032)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37032

Before, calling into global.turboModuleProxy would kickstart the  module creation algorithm, **even if the module wasn't registered.**

Now, if the module isn't registered, TurboModuleManager will just early return null.

NOTE: When an unregistered module is requested from Java via TurboModuleManager.getModule(moduleName), the module algorithm will **still** run.

This fixes a bug:
- global.**native**ModuleProxy will no longer kickstart **turbo** module creation.
- global.**turbo**ModuleProxy will no longer kickstart **legacy** module creation.

NOTE: This **might** improve fb4a performance **a bit**: The TurboModule creation algorithm is *probably* expensive to run. 44 NativeModules are loaded at startup by Fb4a; 8 of them aren't registered with the app: [pastry](https://www.internalfb.com/phabricator/paste/view/P701125588?lines=2%2C4%2C5%2C7%2C9%2C16%2C18%2C24). Those 8 NativeModule creates will now shortcircuit to null faster.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D45195578

fbshipit-source-id: cb09bdc059b3651b02447b7c2e37ef3f4ca2f92b
This commit is contained in:
Ramanpreet Nara
2023-04-21 16:37:04 -07:00
committed by Facebook GitHub Bot
parent 215e574373
commit af5c2d2c75
3 changed files with 72 additions and 4 deletions
@@ -168,6 +168,26 @@ public abstract class ReactPackageTurboModuleManagerDelegate extends TurboModule
return (TurboModule) resolvedModule;
}
public boolean unstable_isModuleRegistered(String moduleName) {
for (final ModuleProvider moduleProvider : mModuleProviders) {
final ReactModuleInfo moduleInfo = mPackageModuleInfos.get(moduleProvider).get(moduleName);
if (moduleInfo != null && moduleInfo.isTurboModule()) {
return true;
}
}
return false;
}
public boolean unstable_isLegacyModuleRegistered(String moduleName) {
for (final ModuleProvider moduleProvider : mModuleProviders) {
final ReactModuleInfo moduleInfo = mPackageModuleInfos.get(moduleProvider).get(moduleName);
if (moduleInfo != null && !moduleInfo.isTurboModule()) {
return true;
}
}
return false;
}
@Nullable
@Override
public NativeModule getLegacyModule(String moduleName) {
@@ -38,6 +38,7 @@ public class TurboModuleManager implements JSIModule, TurboModuleRegistry {
private final List<String> mEagerInitModuleNames;
private final ModuleProvider mTurboModuleProvider;
private final ModuleProvider mLegacyModuleProvider;
private final TurboModuleManagerDelegate mDelegate;
// Prevents the creation of new TurboModules once cleanup as been initiated.
private final Object mModuleCleanupLock = new Object();
@@ -59,6 +60,7 @@ public class TurboModuleManager implements JSIModule, TurboModuleRegistry {
CallInvokerHolder jsCallInvokerHolder,
CallInvokerHolder nativeCallInvokerHolder) {
maybeLoadSoLibrary();
mDelegate = delegate;
mHybridData =
initHybrid(
runtimeExecutor,
@@ -93,6 +95,14 @@ public class TurboModuleManager implements JSIModule, TurboModuleRegistry {
};
}
private boolean isTurboModule(String moduleName) {
return mDelegate != null && mDelegate.unstable_isModuleRegistered(moduleName);
}
private boolean isLegacyModule(String moduleName) {
return mDelegate != null && mDelegate.unstable_isLegacyModuleRegistered(moduleName);
}
private static boolean shouldCreateLegacyModules() {
return ReactFeatureFlags.enableBridgelessArchitecture
&& ReactFeatureFlags.unstable_useTurboModuleInterop;
@@ -117,12 +127,20 @@ public class TurboModuleManager implements JSIModule, TurboModuleRegistry {
@DoNotStrip
@Nullable
private NativeModule getLegacyJavaModule(String moduleName) {
final NativeModule module = getModule(moduleName);
if (shouldRouteTurboModulesThroughInteropLayer()) {
final NativeModule module = getModule(moduleName);
return !(module instanceof CxxModuleWrapper) ? module : null;
}
/*
* This API is invoked from global.nativeModuleProxy.
* Only call getModule if the native module is a legacy module.
*/
if (!isLegacyModule(moduleName)) {
return null;
}
final NativeModule module = getModule(moduleName);
return !(module instanceof CxxModuleWrapper) && !(module instanceof TurboModule)
? module
: null;
@@ -132,12 +150,20 @@ public class TurboModuleManager implements JSIModule, TurboModuleRegistry {
@DoNotStrip
@Nullable
private CxxModuleWrapper getLegacyCxxModule(String moduleName) {
final NativeModule module = getModule(moduleName);
if (shouldRouteTurboModulesThroughInteropLayer()) {
final NativeModule module = getModule(moduleName);
return module instanceof CxxModuleWrapper ? (CxxModuleWrapper) module : null;
}
/*
* This API is invoked from global.nativeModuleProxy.
* Only call getModule if the native module is a legacy module.
*/
if (!isLegacyModule(moduleName)) {
return null;
}
final NativeModule module = getModule(moduleName);
return module instanceof CxxModuleWrapper && !(module instanceof TurboModule)
? (CxxModuleWrapper) module
: null;
@@ -151,6 +177,14 @@ public class TurboModuleManager implements JSIModule, TurboModuleRegistry {
return null;
}
/*
* This API is invoked from global.__turboModuleProxy.
* Only call getModule if the native module is a turbo module.
*/
if (!isTurboModule(moduleName)) {
return null;
}
final NativeModule module = getModule(moduleName);
return module instanceof CxxModuleWrapper && module instanceof TurboModule
? (CxxModuleWrapper) module
@@ -164,6 +198,14 @@ public class TurboModuleManager implements JSIModule, TurboModuleRegistry {
return null;
}
/*
* This API is invoked from global.__turboModuleProxy.
* Only call getModule if the native module is a turbo module.
*/
if (!isTurboModule(moduleName)) {
return null;
}
final NativeModule module = getModule(moduleName);
return !(module instanceof CxxModuleWrapper) && module instanceof TurboModule
? (TurboModule) module
@@ -38,6 +38,8 @@ public abstract class TurboModuleManagerDelegate {
@Nullable
public abstract TurboModule getModule(String moduleName);
public abstract boolean unstable_isModuleRegistered(String moduleName);
/**
* Create an return a legacy NativeModule with name `moduleName`. If `moduleName` is a
* TurboModule, return null.
@@ -47,6 +49,10 @@ public abstract class TurboModuleManagerDelegate {
return null;
}
public boolean unstable_isLegacyModuleRegistered(String moduleName) {
return false;
};
public List<String> getEagerInitModuleNames() {
return new ArrayList<>();
}