Skip to content

Cache PreparedLayout instances in Java on Android - #60599

Open
Abbondanzo wants to merge 2 commits into
react:mainfrom
Abbondanzo:export-D136396204
Open

Abbondanzo wants to merge 2 commits into
react:mainfrom
Abbondanzo:export-D136396204

Conversation

@Abbondanzo

@Abbondanzo Abbondanzo commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

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.

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):

Metric Default deferPreparedLayoutReactTagsToMountAndroid useJavaPreparedLayoutCacheAndroid
End-of-run JNI globals 3,724 683 (-81.7%) 483 (-87.0%)
JS thread CPU 2,935 ms 2,865 ms (-2.4%) 2,845 ms (-3.1%)
Main (UI) thread CPU 1,595 ms 1,570 ms (-1.6%) 1,620 ms (+1.6%)
GC thread CPU 140 ms 135 ms (-3.6%) 130 ms (-7.1%)
Total CPU 5,000 ms 4,890 ms (-2.2%) 4,920 ms (-1.6%)
Wall elapsed 4,366.5 ms 4,268.0 ms (-2.3%) 4,306.0 ms (-1.4%)

Cache-miss unique churn (40 ticks × 60 rows × 2 unique <Text> = 4,800 unique prepare+measure+mount cycles/launch):

Metric Default deferPreparedLayoutReactTagsToMountAndroid useJavaPreparedLayoutCacheAndroid
End-of-run JNI globals 4,110 4,134 (+0.6%) 483 (-88.2%)
JS thread CPU 1,805 ms 1,800 ms (-0.3%) 1,785 ms (-1.1%)
Main (UI) thread CPU 735 ms 730 ms (-0.7%) 730 ms (-0.7%)
GC thread CPU 95 ms 100 ms (+5.3%) 90 ms (-5.3%)
RenderThread CPU 115 ms 120 ms (+4.3%) 110 ms (-4.3%)
Total CPU 2,745 ms 2,755 ms (+0.4%) 2,740 ms (-0.2%)
Wall elapsed 2,251.0 ms 2,227.5 ms (-1.0%) 2,240.5 ms (-0.5%)
Frame time p50 / p90 / p95 8 / 12 / 14 ms 8 / 11 / 12.5 ms 8.5 / 11 / 13 ms

ART HPROF verification (RNTester debug): PreparedLayout JNI global roots dropped to 0 across Static 1,000 (vs. 63), Unique 300 with preparedTextCacheSize = 200 (vs. 353), and Unique 300 under forced eviction (preparedTextCacheSize = 10) with 100% of views mounted.

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

…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
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 5, 2026
@meta-codesync

meta-codesync Bot commented Dec 5, 2026

Copy link
Copy Markdown

@Abbondanzo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D136396204.

@github-actions github-actions Bot removed the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant