Parse each Java TurboModule method's JNI signature once

## Summary:

This is the Android counterpart of #59394.

On Android, every call from JS to a Java TurboModule method goes through `JavaTurboModule::invokeJavaMethod` (`ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.cpp`). On every call it parsed the method's JNI signature into a new `std::vector<std::string>` of argument types (`getMethodArgTypesFromSignature`), and for methods that return a number or a boolean it cut the return type out of the signature again with `substr`. Long type names such as `Lcom/facebook/react/bridge/ReadableMap;` don't fit in the small-string buffer, so each of them was a heap allocation on every call.

A method's signature doesn't change, so this change parses it on the method's first call and keeps the result. Each `JavaTurboModule` now has one `MethodInfo` per method (the argument types and the return type), keyed by the method's `jmethodID`, which is already resolved at that point. A mutex guards the map. Entries are never removed while the module is alive, and `std::unordered_map` doesn't move its elements, so the returned reference stays valid for the rest of the call. The members are private, so there is no API change.

Memory: one entry per method that is actually called, about 150–300 bytes each (the type strings and the map node). The same strings were already allocated on every call and freed right after.

One side effect: `JavaTurboModule` now holds a `std::mutex`, so it can't be copied or moved. Copying a module was already deprecated (it has a user-declared destructor) and nothing in the repository copies one; generated modules are created with `std::make_shared`.

## Changelog:

[ANDROID] [CHANGED] - Parse each TurboModule method's JNI signature once instead of on every call

## Test Plan:

- `clang-format --dry-run --Werror` (the repository's pinned version) on the changed files passes.
- RNTester built with Gradle in Release (`assembleRelease`, arm64-v8a) and run on a physical Samsung Galaxy A14 (Android 15). In the TurboModule example, "Run function call tests" returns the same values for all 18 methods with and without this change, including callbacks, a resolved and a rejected promise, and ArrayBuffers.
- To run `SampleTurboModule` in a Release build I had to change two things locally, in the sample module only and in both builds:
  - In Release, creating it fails with `A launcher is already registered for key 'com.facebook.fbreact.specs.SampleTurboModule:e2'`. The launcher key is built from the contract class names, and R8 seems to merge the contract classes, so the three launchers get the same key. I passed an explicit key to each `registerForActivityResult` call.
  - It shows a `Toast` on every call, so I turned that off for the measurement.
- **Measurement.** RNTester Playground calling `NativeSampleTurboModule` methods in a loop (20,000 calls each for the sync methods, 5,000 for the callback), timed with `performance.now()`. Two Release APKs, before and after this change, installed in the order before, after, after, before, 4 launches each after a warm-up launch. The presses were driven by [Argent](https://github.com/software-mansion/argent) on the physical phone. Mean time per call:

| Call | before | this PR | change |
|---|--:|--:|--:|
| `getString` | 1.93 µs | 1.66 µs | −14% |
| `getValue` (number, string, object) | 18.96 µs | 17.62 µs | −7% |
| `getValueWithCallback` | 11.46 µs | 10.66 µs | −7% |
| `getArray` (3 items) | 15.84 µs | 14.83 µs | −6% |
| `getObject` (small) | 14.02 µs | 13.19 µs | −6% |

For these rows, the ranges of the two arms don't overlap. `getNumber` (1.28 → 1.14 µs), `getBool`, the 20-key `getObject`, `voidFunc` and promises were within noise in this run. An earlier run of the same change behind a switch in one APK gave −5% to −20% for these calls and `getNumber` (−20%).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Differential Revision: D128040903

Pull Request resolved: https://github.com/react/react-native/pull/59414
This commit is contained in:
Gregory Moskaliuk authored and GitHub committed 2026-10-29 11:35:05 +00:00
1 parent 745b4f355e
commit f7de20301c
2 files changed
+34 -7

No files matched your search

@@ -588,6 +588,24 @@ void rejectWithException(
} // namespace
const JavaTurboModule::MethodInfo& JavaTurboModule::getMethodInfo(
jmethodID methodID,
const std::string& methodSignature) {
std::lock_guard<std::mutex> lock(methodInfosMutex_);
auto entry = methodInfos_.find(methodID);
if (entry == methodInfos_.end()) {
entry = methodInfos_
.emplace(
methodID,
MethodInfo{
getMethodArgTypesFromSignature(methodSignature),
methodSignature.substr(
methodSignature.find_last_of(')') + 1)})
.first;
}
return entry->second;
}
jsi::Value JavaTurboModule::invokeJavaMethod(
jsi::Runtime& runtime,
TurboModuleMethodValueKind valueKind,
@@ -689,14 +707,13 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
return returnValue;
}
std::vector<std::string> methodArgTypes =
getMethodArgTypesFromSignature(methodSignature);
const MethodInfo& methodInfo = getMethodInfo(methodID, methodSignature);
JNIArgs jniArgs = convertJSIArgsToJNIArgs(
env,
runtime,
methodNameStr,
methodArgTypes,
methodInfo.argTypes,
args,
argCount,
jsInvoker_,
@@ -712,8 +729,7 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
switch (valueKind) {
case BooleanKind: {
std::string returnType =
methodSignature.substr(methodSignature.find_last_of(')') + 1);
const std::string& returnType = methodInfo.returnType;
if (returnType == "Ljava/lang/Boolean;") {
auto returnObject =
env->CallObjectMethodA(instance, methodID, jargs.data());
@@ -749,8 +765,7 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
}
}
case NumberKind: {
std::string returnType =
methodSignature.substr(methodSignature.find_last_of(')') + 1);
const std::string& returnType = methodInfo.returnType;
if (returnType == "Ljava/lang/Double;" ||
returnType == "Ljava/lang/Float;" ||
returnType == "Ljava/lang/Integer;") {
@@ -9,7 +9,10 @@
#include <react/cxxstableapi/UmbrellaGuard.h>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
#include <React/Bridging.h>
#include <React/CallInvoker.h>
@@ -61,6 +64,15 @@ class JSI_EXPORT JavaTurboModule : public TurboModule {
// instance_ can be of type JTurboModule, or JNativeModule
jni::global_ref<jobject> instance_;
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker_;
// A method's JNI signature, parsed on its first call since it doesn't change.
struct MethodInfo {
std::vector<std::string> argTypes;
std::string returnType;
};
std::unordered_map<jmethodID, MethodInfo> methodInfos_;
std::mutex methodInfosMutex_;
const MethodInfo &getMethodInfo(jmethodID methodID, const std::string &methodSignature);
};
} // namespace facebook::react