Skip to content

Enable synchronous void method invocations via feature flag - RFC#56740

Open
christophpurrer wants to merge 1 commit intofacebook:mainfrom
christophpurrer:export-D104331837
Open

Enable synchronous void method invocations via feature flag - RFC#56740
christophpurrer wants to merge 1 commit intofacebook:mainfrom
christophpurrer:export-D104331837

Conversation

@christophpurrer
Copy link
Copy Markdown
Contributor

Summary:
Introduce a feature flag enableSyncVoidMethods that allows TurboModule methods with void return types to be invoked synchronously on the JS thread instead of being dispatched asynchronously.

The flag only applies to pure TurboModules (not interop modules) to avoid impacting backward compatibility with legacy Native Modules. When enabled, void methods behave like other synchronous methods, improving performance for operations that don't need async dispatch.

Behavior mismatch

Currently:

  • C++ Turbo Modules run void methods sync
  • Java / ObjC Turbo Modules run void methods async

Rationale

JavaScript code like this

      const buffer = new ArrayBuffer(4);
      const view = new Uint8Array(buffer);
      view[0] = 1;
      view[1] = 2;
      view[2] = 3;
      view[3] = 4;
      const result = NativeCxxModuleExample.passArrayBuffer(buffer);
      /** In native code do something like:
      * std::span<uint8_t> bytes(arg.data(rt), arg.size(rt));
      * std::reverse(bytes.begin(), bytes.end());
      **/
      // ArrayBuffer was Shared with native code and value updates are reflected to JS code
      // view[0] = 4;
      // view[1] = 3;
      // view[2] = 2;
      // view[3] = 1;

for a TM spec as

 +passArrayBuffer: (arg: ArrayBuffer) => void;

works for C++ Turbo Modules
see: #56690

but currently fails for Java / Objc Turbo Modules as calling

const result = NativeCxxModuleExample.passArrayBuffer(buffer); // 

is done async for Java/ObjC Modules and hence we would need to copy the ArrayBuffer in native code.

Changelog: [Internal]

Differential Revision: D104331837

Summary:
Introduce a feature flag `enableSyncVoidMethods` that allows TurboModule methods with void return types to be invoked synchronously on the JS thread instead of being dispatched asynchronously.

The flag only applies to pure TurboModules (not interop modules) to avoid impacting backward compatibility with legacy Native Modules. When enabled, void methods behave like other synchronous methods, improving performance for operations that don't need async dispatch.

# Behavior mismatch
Currently:
- C++ Turbo Modules run void methods `sync`
- Java / ObjC Turbo Modules run void methods `async`

# Rationale
JavaScript code like this
```
      const buffer = new ArrayBuffer(4);
      const view = new Uint8Array(buffer);
      view[0] = 1;
      view[1] = 2;
      view[2] = 3;
      view[3] = 4;
      const result = NativeCxxModuleExample.passArrayBuffer(buffer);
      /** In native code do something like:
      * std::span<uint8_t> bytes(arg.data(rt), arg.size(rt));
      * std::reverse(bytes.begin(), bytes.end());
      **/
      // ArrayBuffer was Shared with native code and value updates are reflected to JS code
      // view[0] = 4;
      // view[1] = 3;
      // view[2] = 2;
      // view[3] = 1;
```
for a TM spec as
```
 +passArrayBuffer: (arg: ArrayBuffer) => void;
```
works for C++ Turbo Modules
see: facebook#56690

but currently fails for Java / Objc Turbo Modules as calling
```
const result = NativeCxxModuleExample.passArrayBuffer(buffer); // 
```
is done `async` for Java/ObjC Modules and hence we would need to copy the `ArrayBuffer` in native code.


Changelog: [Internal]

Differential Revision: D104331837
@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 May 8, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented May 8, 2026

@christophpurrer has exported this pull request. If you are a Meta employee, you can view the originating Diff in D104331837.

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. fb-exported meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant