Repository navigation
Cache PreparedLayout instances in Java on Android - #60599
Open
Abbondanzo wants to merge 2 commits into
Open
Abbondanzo wants to merge 2 commits into
Abbondanzo wants to merge 2 commits into
Conversation
…60579) Summary: Pull Request resolved: react#60579 When `enablePreparedTextLayout` is enabled on Android, a `preparedTextCache_` hit whose fragment React tags differ from the cached entry currently calls `reusePreparedLayoutWithNewReactTags` during `TextLayoutManager::prepareLayout` and wraps the cloned `PreparedLayout` in a brand-new `jni::global_ref`. Under heavy list/navigation churn or screens with repeated text strings, each tag-divergent `ParagraphShadowNode` holds an uncached JNI global reference for its lifetime, multiplying global-reference table usage well beyond `preparedTextCacheSize`. Add the `deferPreparedLayoutReactTagsToMountAndroid` feature flag (default `false`). When enabled, `TextLayoutManager::prepareLayout` shares the cached `PreparedTextLayout` global reference across tag-divergent nodes (marking it as having divergent tags) without crossing JNI or allocating a new global reference on the layout thread. At mount time, `ParagraphState::getJNIReference` attaches the node's fragment tags via `PreparedLayout.withReactTags` and returns a `jni::local_ref`, so the tag-specific wrapper lives strictly on the Java heap until the view drops or recycles it. Changelog: [Android][Changed] - Add `deferPreparedLayoutReactTagsToMountAndroid` feature flag to share cached `PreparedLayout` JNI global references across differing React tags and attach new tags via a local reference on mount Signed-off-by: Peter Abbondanzo <abbo@meta.com> Differential Revision: D136369766
Summary: Even when cached `PreparedLayout` JNI global references are shared across differing React tags, C++ `preparedTextCache_` and live `ParagraphShadowNode` / `ParagraphState` instances still retain one `jni::global_ref` per unique layout entry, pinning both JNI global-reference slots and Java `StaticLayout` objects even after cache eviction if shadow nodes stay alive. Add the `useJavaPreparedLayoutCacheAndroid` feature flag (default `false`) to supersede `deferPreparedLayoutReactTagsToMountAndroid`. When enabled, `FabricUIManager` owns a bounded Java-side `PreparedLayoutCache` (`LruCache<Long, PreparedLayout>`, cleared on `invalidate()`), while C++ `PreparedTextLayout` stores a shared handle holding a C++-allocated `int64_t` cache ID, cached `TextMeasurement`, and fallback inputs. Cache misses prepare, insert into the Java cache, and measure in a single fused `prepareAndMeasureTextLayoutToCache` JNI call (eliminating all `PreparedLayout` JNI global references and halving cache-miss JNI crossings), and cache hits skip JNI entirely during both `prepareLayout` and `measurePreparedLayout` (falling back to `doMeasure` if re-measured under different constraints after eviction). At mount time, `ParagraphState::getJNIReference` resolves the cache ID into a `jni::local_ref` (attaching divergent fragment tags via `PreparedLayout.withReactTags`), and transparently re-prepares and re-caches under the same `cacheId` via `prepareTextLayoutAndCache` if the entry was evicted before mount. Changelog: [Android][Changed] - Add `useJavaPreparedLayoutCacheAndroid` feature flag to store `PreparedLayout` instances in a bounded Java LRU cache keyed by opaque ID with zero JNI global references Signed-off-by: Peter Abbondanzo <abbo@meta.com> Differential Revision: D136396204
|
@Abbondanzo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D136396204. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Even when cached
PreparedLayoutJNI global references are shared across differing React tags, C++preparedTextCache_and liveParagraphShadowNode/ParagraphStateinstances still retain onejni::global_refper unique layout entry, pinning both JNI global-reference slots and JavaStaticLayoutobjects even after cache eviction if shadow nodes stay alive.Add the
useJavaPreparedLayoutCacheAndroidfeature flag (defaultfalse) to supersededeferPreparedLayoutReactTagsToMountAndroid. When enabled,FabricUIManagerowns a bounded Java-sidePreparedLayoutCache(LruCache<Long, PreparedLayout>, cleared oninvalidate()), while C++PreparedTextLayoutstores a shared handle holding a C++-allocatedint64_tcache ID, cachedTextMeasurement, and fallback inputs. Cache misses prepare, insert into the Java cache, and measure in a single fusedprepareAndMeasureTextLayoutToCacheJNI call (eliminating allPreparedLayoutJNI global references and halving cache-miss JNI crossings), and cache hits skip JNI entirely during bothprepareLayoutandmeasurePreparedLayout(falling back todoMeasureif re-measured under different constraints after eviction). At mount time,ParagraphState::getJNIReferenceresolves the cache ID into ajni::local_ref(attaching divergent fragment tags viaPreparedLayout.withReactTags), and transparently re-prepares and re-caches under the samecacheIdviaprepareTextLayoutAndCacheif the entry was evicted before mount.Benchmarks (
RNTester, Android arm64 Release, AOT speed-compiled)Single-binary runtime flag toggle, 1 discarded warmup + 4 alternating cold launches per arm (medians).
Cache-hit tag churn (120 ticks × 60 rows × 2 repeated
<Text>= 14,400 mounts/launch):deferPreparedLayoutReactTagsToMountAndroiduseJavaPreparedLayoutCacheAndroidCache-miss unique churn (40 ticks × 60 rows × 2 unique
<Text>= 4,800 unique prepare+measure+mount cycles/launch):deferPreparedLayoutReactTagsToMountAndroiduseJavaPreparedLayoutCacheAndroidART HPROF verification (
RNTesterdebug):PreparedLayoutJNI global roots dropped to 0 acrossStatic 1,000(vs. 63),Unique 300withpreparedTextCacheSize = 200(vs. 353), andUnique 300under forced eviction (preparedTextCacheSize = 10) with 100% of views mounted.Changelog:
[Android][Changed] - Add
useJavaPreparedLayoutCacheAndroidfeature flag to storePreparedLayoutinstances in a bounded Java LRU cache keyed by opaque ID with zero JNI global referencesSigned-off-by: Peter Abbondanzo abbo@meta.com
Differential Revision: D136396204