Skip to content

Key Android text caches on paragraph base paint attributes - #60463

Closed
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/android-text-cache-base-paint
Closed

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/android-text-cache-base-paint

Conversation

@ide

@ide ide commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary:

Fabric on Android initializes its measurement paint from the paragraph's base attributes. Those attributes can differ after nested text overrides them in every fragment. Two paragraphs with identical fragments and widths can then share a line measurement even though their paints produce different capHeight and xHeight values.

Include the base font and spacing inputs in the Android size, line and prepared-layout keys. The iOS size and line keys still reuse measurements when their fragments match. For empty iOS strings, the placeholder fragment already carries the base attributes.

Before After
The 44 dp outer paragraph inherits the 20 dp metrics at the same width The 44 dp outer paragraph reports 31.8 dp capHeight and 23.5 dp xHeight

Changelog:

[ANDROID] [FIXED] - Use the paragraph's base paint when caching Fabric text measurements.

Test Plan:

Built the Fantom tester target and ran its CTest suite.

./gradlew :private:react-native-fantom:configureFantomTester --console=plain --max-workers=5
cmake --build private/react-native-fantom/build/tester --target react_renderer_textlayoutmanager_tests -j 6
ctest --test-dir private/react-native-fantom/build/tester/react-common-tests \
  -R '^react/renderer/textlayoutmanager/tests/' --output-on-failure

The C++ regressions cover every base paint field in the size, line and prepared keys. They detect missing base comparisons, missing hash contributions and applying the base to the shared iOS keys.

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --rerun \
  --tests 'com.facebook.react.views.text.*' --console=plain --max-workers=4
./gradlew :packages:rn-tester:android:app:assembleDebug \
  -PreactNativeArchitectures=arm64-v8a --console=plain --max-workers=4

In RNTester on Android 16, the first sample uses a 20 dp outer font at 280 dp width and primes the line cache. The second uses a 44 dp outer font at the same width. Both render 20 dp inner text. Separate-width samples expose each paragraph's metrics on a fresh key. The 44 dp sample at the shared width changes from 14.5 and 10.7 dp to 31.8 and 23.5 dp, matching the separate-width sample. RNTester exercises the Android line cache. The Fantom tests cover the size and prepared keys. Before capture · After capture.

Apple Clang -O3, Apple M6. Median retired instructions per full key comparison or hash, over five warmed repetitions. The comparison uses two equal copies of each key. The benchmark uses the Fantom C++ build's types and enables the Android base-paint field for the Android rows.

Key path Fragments Equality before → after Hash before → after
Android size 1 456.04 → 671.06 370.03 → 549.06
Android size 4 1365.05 → 1580.13 1108.04 → 1293.10
Android lines 1 436.03 → 649.06 345.03 → 524.05
Android lines 4 1345.08 → 1558.10 1083.04 → 1268.07
Android prepared 1 697.04 → 898.07 528.05 → 700.07
Android prepared 4 2332.12 → 2536.22 1731.07 → 1909.15
iOS size path 1 456.03 → 469.06 370.07 → 374.05
iOS size path 4 1365.05 → 1378.11 1108.10 → 1118.11
iOS line path 1 436.08 → 447.06 345.03 → 349.05
iOS line path 4 1345.14 → 1356.12 1083.06 → 1093.08

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 2, 2026
@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 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at c4212c79bbf2.

Nested text can have identical fragments and different base paint. Android initializes the paint from the paragraph's base attributes, so onTextLayout can report the smaller paragraph's capHeight and xHeight for both paragraphs.

Compare and hash the attributes that initialize Android's paint in the size, line and prepared-layout keys. Android opts into the base paint for the shared size and line keys so matching iOS fragments can still reuse their measurements.

The Fantom text layout tests exercise the three keys and the shared keys without base paint. The Android text unit tests pass. RNTester reports distinct line metrics for identical inner text at the same width.
@ide
ide force-pushed the @ide/android-text-cache-base-paint branch from e49693f to c4212c7 Compare December 2, 2026 12:20
@ide
ide marked this pull request as ready for review December 2, 2026 13:45
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 2, 2026
@ide
ide marked this pull request as draft December 2, 2026 15:40
@ide

ide commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Closing without merging. Comparing and hashing the paragraph's base paint attributes in every Android text cache key costs 29–48% more instructions per lookup, which is too much for this case.

This PR stays as a record of the defect: Android measures with the paragraph's base paint, which none of the size, line or prepared-layout keys include. Two paragraphs whose fragments match but whose outer font differs can therefore share a cached line measurement. In the example above, a 44 dp paragraph reports the 20 dp paragraph's capHeight and xHeight (14.5 and 10.7 dp instead of 31.8 and 23.5 dp) in onTextLayout. A fix would need to keep the key cost flat.

@ide ide closed this Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant