From 75e5b2dc9a215784aae3c1a525372997604ae444 Mon Sep 17 00:00:00 2001 From: James Ide Date: Fri, 4 Dec 2026 10:44:12 -0800 Subject: [PATCH] Reuse Java component name strings across Android mount items `FabricMountingManager` created a Java string with `NewStringUTF` for the component name of every Create mount item and every preallocated view. It now creates one string per name, keeps it as a global reference until the `FabricMountingManager` is destroyed, and passes that string each time. ReactAndroid builds, and RNTester's Release build mounts its screens on an arm64 Android 16 emulator. On that emulator, a JNI benchmark in RNTester that looks up names in the order View, View, Paragraph, View, Image, View measured a median of 81.3 to 83.9 ns per name with `make_jstring` and 7.1 to 7.6 ns with the cache, over three rounds of 21 samples. Signed-off-by: James Ide --- .../react/fabric/FabricMountingManager.cpp | 56 ++++++++++++------- .../jni/react/fabric/FabricMountingManager.h | 12 ++++ 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp index c20cefc00913..ae2e5e2832f5 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp @@ -42,9 +42,13 @@ FabricMountingManager::FabricMountingManager( : javaUIManager_(javaUIManager) {} FabricMountingManager::~FabricMountingManager() { - // Manually reset `javaUIManager_` since FabricMountingManager may be retained - // from a GC thread (destroyUnmountedShadowNode) - jni::ThreadScope::WithClassLoader([&]() { javaUIManager_.reset(); }); + // Release the Java references in a JNI thread scope, since + // FabricMountingManager may be retained from a GC thread + // (destroyUnmountedShadowNode) + jni::ThreadScope::WithClassLoader([&]() { + javaUIManager_.reset(); + componentNames_.clear(); + }); } void FabricMountingManager::onSurfaceStart(SurfaceId surfaceId) { @@ -291,20 +295,6 @@ inline std::pair computeBufferSizes( return std::make_pair(batchMountItemIntsSize, batchMountItemObjectsSize); } -// TODO: this method will be removed when binding for components are code-gen -jni::local_ref getPlatformComponentName(const ShadowView& shadowView) { - constexpr static std::string_view scrollViewComponentName = "ScrollView"; - - if (scrollViewComponentName == shadowView.componentName) { - const auto& newViewProps = - static_cast(*shadowView.props); - if (newViewProps.getProbablyMoreHorizontalThanVertical_DEPRECATED()) { - return jni::make_jstring("AndroidHorizontalScrollView"); - } - } - return jni::make_jstring(shadowView.componentName); -} - inline float scale(Float value, Float pointScaleFactor) { std::feclearexcept(FE_ALL_EXCEPT); float result = value * pointScaleFactor; @@ -424,14 +414,13 @@ inline void writeMountItemPreamble( inline void writeCreateMountItem( InstructionBuffer& buffer, - const CppMountItem& mountItem) { + const CppMountItem& mountItem, + jni::alias_ref componentName) { int isLayoutable = mountItem.newChildShadowView.layoutMetrics != EmptyLayoutMetrics ? 1 : 0; buffer.writeIntArray( std::array{mountItem.newChildShadowView.tag, isLayoutable}); - auto componentName = getPlatformComponentName(mountItem.newChildShadowView); - jni::local_ref props = getProps(mountItem.oldChildShadowView, mountItem.newChildShadowView); @@ -589,6 +578,28 @@ inline void writeUpdateOverflowInsetMountItem( } // namespace +// TODO: this method will be removed when binding for components are code-gen +jni::alias_ref FabricMountingManager::getPlatformComponentName( + const ShadowView& shadowView) { + const char* name = shadowView.componentName; + if (std::string_view{name} == "ScrollView") { + const auto& scrollViewProps = + static_cast(*shadowView.props); + if (scrollViewProps.getProbablyMoreHorizontalThanVertical_DEPRECATED()) { + name = "AndroidHorizontalScrollView"; + } + } + + std::lock_guard lock(componentNamesMutex_); + auto it = componentNames_.find(std::string_view{name}); + if (it == componentNames_.end()) { + it = + componentNames_.emplace(name, jni::make_global(jni::make_jstring(name))) + .first; + } + return it->second; +} + void FabricMountingManager::executeMount( const MountingTransaction& transaction, bool synchronous) { @@ -970,7 +981,10 @@ void FabricMountingManager::executeMount( switch (mountItemType) { case CppMountItem::Type::Create: - writeCreateMountItem(buffer, mountItem); + writeCreateMountItem( + buffer, + mountItem, + getPlatformComponentName(mountItem.newChildShadowView)); break; case CppMountItem::Type::Delete: writeDeleteMountItem(buffer, mountItem); diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h index b835826de4d1..73369c1343fc 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h @@ -9,10 +9,12 @@ #include #include +#include #include #include #include +#include #include #include @@ -108,8 +110,18 @@ class FabricMountingManager final { void preallocateRegisteredView(const ShadowView &shadowView); + /* + * Returns the component name Java looks the view's manager up by. Each name + * is one Java string, created on first use and kept for the lifetime of this + * FabricMountingManager. + */ + jni::alias_ref getPlatformComponentName(const ShadowView &shadowView); + jni::global_ref javaUIManager_; + folly::F14FastMap> componentNames_{}; + std::mutex componentNamesMutex_; + std::recursive_mutex commitMutex_; /*