Skip to content

Require BatchedBridge lazily in NativeModules - #58435

Open
shubh73 wants to merge 1 commit into
react:mainfrom
shubh73:lazy-batched-bridge-require
Open

Require BatchedBridge lazily in NativeModules#58435
shubh73 wants to merge 1 commit into
react:mainfrom
shubh73:lazy-batched-bridge-require

Conversation

@shubh73

@shubh73 shubh73 commented Sep 10, 2026

Copy link
Copy Markdown

Summary:

NativeModules requires BatchedBridge at module scope, which constructs a MessageQueue and defines global.__fbBatchedBridge. In bridgeless mode the module resolves to global.nativeModuleProxy and the bridge is never read again: its only users are genModule and genMethod, which run from the __fbBatchedBridgeConfig branch or via __fbGenNativeModule from the legacy executor.

Bundles built with inlineRequires (the default in @react-native/metro-config) already defer this require. Bundles built without it, such as those from @expo/metro-config, evaluate BatchedBridge and MessageQueue at startup with no consumer. This moves the require into a cached getter, matching the RN$Bridgeless guards in registerCallableModule and JSTimers. Bridge mode is unchanged, and bundle size is unchanged since Metro still follows the require.

Changelog:

[GENERAL] [CHANGED] - Require BatchedBridge lazily from NativeModules so bridgeless startup no longer instantiates MessageQueue.

Test Plan:

  • yarn test packages/react-native/Libraries/BatchedBridge packages/react-native/Libraries/TurboModule packages/react-native/Libraries/Core packages/react-native/Libraries/ReactNative — 6 suites, 102 tests passed.
  • yarn flow-check — 0 errors. ESLint and Prettier clean on the file.
  • private/helloworld dev bundle with inlineRequires: false, evaluated in a Node VM with bridgeless globals stubbed: 191 → 189 modules; BatchedBridge.js and MessageQueue.js no longer evaluate, nothing added.

🤖 Generated with Claude Code

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 10, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Sep 10, 2026
`NativeModules` requires `BatchedBridge` at module scope, which constructs
a `MessageQueue` and defines `global.__fbBatchedBridge`. In bridgeless mode
the module resolves to `global.nativeModuleProxy` and the bridge is never
read again: its only users are `genModule` and `genMethod`, which run from
the `__fbBatchedBridgeConfig` branch or via `__fbGenNativeModule` from the
legacy executor.

Bundles built with `inlineRequires` (the default in
`@react-native/metro-config`) already defer this require. Bundles built
without it, such as those from `@expo/metro-config`, evaluate
`BatchedBridge` and `MessageQueue` at startup with no consumer. Move the
require into a cached getter, matching the `RN$Bridgeless` guards in
`registerCallableModule` and `JSTimers`.

Changelog: [GENERAL] [CHANGED] - Require `BatchedBridge` lazily from `NativeModules` so bridgeless startup no longer instantiates `MessageQueue`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@shubh73
shubh73 force-pushed the lazy-batched-bridge-require branch from 61eb969 to b0666a3 Compare September 10, 2026 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant