Skip to content

Defer PreparedLayout reactTags attachment to mount on Android (#60579) - #60579

Open
Abbondanzo wants to merge 1 commit into
react:mainfrom
Abbondanzo:export-D136369766
Open

Abbondanzo wants to merge 1 commit into
react:mainfrom
Abbondanzo:export-D136369766

Conversation

@Abbondanzo

@Abbondanzo Abbondanzo commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

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.

Benchmarks (RNTester, Android arm64 Release, AOT speed-compiled)

Single-binary runtime flag toggle, 1 discarded warmup + 6 alternating cold launches per arm (medians). Static 1,000 mounts 1,000 <Text> views sharing 10 strings; Tag churn remounts 60 rows × 2 <Text> views for 120 ticks (14,400 tag-reuse mounts per run):

Metric Flag off (default) deferPreparedLayoutReactTagsToMountAndroid = true Delta
Static 1,000 total JNI globals 1,533 523 -65.9%
Tag-churn end-of-run JNI globals 3,627 673 -81.4%
Tag-churn JS thread CPU 2,970 ms 2,915 ms -1.9%
Tag-churn main (UI) thread CPU 1,670 ms 1,665 ms -0.3%
Tag-churn total CPU 5,115 ms 5,070 ms -0.9%
Tag-churn wall elapsed 4,455.5 ms 4,443.0 ms -0.3%
Frame time p50 / p90 / p95 9.5 / 13 / 15 ms 10 / 13 / 15 ms 0% p90/p95

ART HPROF verification on Static 1,000 (RNTester debug, Android API 35 x86_64): PreparedLayout JNI global roots dropped from 1,054 → 63 (tag-reuse global roots 991 → 0; all 991 view-held wrappers live purely on the Java heap).

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

@meta-codesync

meta-codesync Bot commented Dec 4, 2026

Copy link
Copy Markdown

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

…60579)

Summary:

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
@meta-codesync meta-codesync Bot changed the title Defer PreparedLayout reactTags attachment to mount on Android Defer PreparedLayout reactTags attachment to mount on Android (#60579) 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