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 c20cefc0091..ae2e5e2832f 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 b835826de4d..73369c1343f 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_; /*