From 8f6aee0df5b728ec4842a4a2b8f902be126f51ee Mon Sep 17 00:00:00 2001 From: David Vacca Date: Tue, 14 Dec 2021 19:31:24 -0800 Subject: [PATCH] Create experiment to increase size of TextMeasureCache and enable in RNPanel apps Summary: Fabric uses a cache where it stores the result of the text measurement in C++ (to avoid unnecessary text measurement that are very costly). This cache has a "max size" of 256 and this size is not enough to store all the texts we have in the screen In my tests, the amount of texts being measured are ~290 and after scrolling many times they increase to 611. This diff increases the size of the TextMeasure to 1024 for users in the experiment. As a result this improves performance of HoverEvents by +5x times (see test plan) changelog: [internal] internal Reviewed By: JoshuaGross Differential Revision: D33112788 fbshipit-source-id: e15feecf0f54da62b252892d37a64fb4ead29e22 --- .../main/java/com/facebook/react/fabric/jni/Binding.cpp | 5 +++++ .../react/renderer/textlayoutmanager/TextLayoutManager.h | 8 ++++++-- ReactCommon/react/utils/SimpleThreadSafeCache.h | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp index 6fe77f17931..d9f1cb088bc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp @@ -599,6 +599,11 @@ void Binding::installFabricUIManager( dispatchPreallocationInBackground_ = reactNativeConfig_->getBool( "react_native_new_architecture:dispatch_preallocation_in_bg"); + contextContainer->insert( + "EnableLargeTextMeasureCache", + reactNativeConfig_->getBool( + "react_fabric:enable_large_text_measure_cache_android")); + auto toolbox = SchedulerToolbox{}; toolbox.contextContainer = contextContainer; toolbox.componentRegistryFactory = componentsRegistry->buildRegistryFunction; diff --git a/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h b/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h index 19ae11b2a7d..c83b81a3359 100644 --- a/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h +++ b/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h @@ -27,7 +27,11 @@ using SharedTextLayoutManager = std::shared_ptr; class TextLayoutManager { public: TextLayoutManager(const ContextContainer::Shared &contextContainer) - : contextContainer_(contextContainer) { + : contextContainer_(contextContainer), + measureCache_{ + contextContainer->at("EnableLargeTextMeasureCache") + ? 1024 + : kSimpleThreadSafeCacheSizeCap} { static auto value = contextContainer->at("MapBufferSerializationEnabled"); mapBufferSerializationEnabled_ = value; @@ -85,7 +89,7 @@ class TextLayoutManager { void *self_; ContextContainer::Shared contextContainer_; bool mapBufferSerializationEnabled_; - TextMeasureCache measureCache_{}; + TextMeasureCache measureCache_; }; } // namespace react diff --git a/ReactCommon/react/utils/SimpleThreadSafeCache.h b/ReactCommon/react/utils/SimpleThreadSafeCache.h index b500535bb7f..56b038931d1 100644 --- a/ReactCommon/react/utils/SimpleThreadSafeCache.h +++ b/ReactCommon/react/utils/SimpleThreadSafeCache.h @@ -22,6 +22,7 @@ template class SimpleThreadSafeCache { public: SimpleThreadSafeCache() : map_{maxSize} {} + SimpleThreadSafeCache(unsigned long size) : map_{size} {} /* * Returns a value from the map with a given key.