Skip to content

Commit

Permalink
Only create TurboModules, if they're registered (#37032)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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
  • Loading branch information
RSNara authored and facebook-github-bot committed Apr 21, 2023
1 parent 215e574 commit af5c2d2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ public TurboModule getModule(String moduleName) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -59,6 +60,7 @@ public TurboModuleManager(
CallInvokerHolder jsCallInvokerHolder,
CallInvokerHolder nativeCallInvokerHolder) {
maybeLoadSoLibrary();
mDelegate = delegate;
mHybridData =
initHybrid(
runtimeExecutor,
Expand Down Expand Up @@ -93,6 +95,14 @@ public TurboModuleManager(
};
}

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;
Expand All @@ -117,12 +127,20 @@ private static List<TurboModuleInteropUtils.MethodDescriptor> getMethodDescripto
@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;
Expand All @@ -132,12 +150,20 @@ private NativeModule getLegacyJavaModule(String moduleName) {
@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;
Expand All @@ -151,6 +177,14 @@ private CxxModuleWrapper getTurboLegacyCxxModule(String moduleName) {
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
Expand All @@ -164,6 +198,14 @@ private TurboModule getTurboJavaModule(String moduleName) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ protected 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.
Expand All @@ -47,6 +49,10 @@ public NativeModule getLegacyModule(String moduleName) {
return null;
}

public boolean unstable_isLegacyModuleRegistered(String moduleName) {
return false;
};

public List<String> getEagerInitModuleNames() {
return new ArrayList<>();
}
Expand Down

0 comments on commit af5c2d2

Please sign in to comment.