Skip to content

Use exact float equality in text measurement cache keys - #60464

Open
ide wants to merge 2 commits into
react:mainfrom
expo:@ide/text-cache-exact-floats
Open

ide wants to merge 2 commits into
react:mainfrom
expo:@ide/text-cache-exact-floats

Conversation

@ide

@ide ide commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Stacked on #60457; review the last commit.

Summary:

The Fabric text keys compare font sizes, paragraph font limits and pixel scales within an epsilon. The hash uses the original values. With libc++, neighboring values can compare equal and still occupy separate entries. An infinite font limit also exposes an eviction failure. The key compares unequal to itself, so the LRU drops its list entry while the map retains it.

Use exact comparison for cache floats and canonicalize their NaN hashes. Prepared keys also handle NaN shadow offsets accepted by Fabric. Exact comparison keeps neighboring finite inputs distinct and makes an infinite limit reusable. Quantizing would merge distinct inputs, which can fall on opposite sides of a measurement boundary. Dropping floats from the hash would leave the epsilon comparison nontransitive. Prepared keys also compare lineBreakMode, which their hash already includes.

Lookup and eviction for adjacent floats, NaNs and infinity

Changelog:

[INTERNAL] [FIXED] - Make Fabric text measurement cache equality agree with hashing.

Test Plan:

./gradlew :private:react-native-fantom:configureFantomTester --console=plain --max-workers=5
cmake --build private/react-native-fantom/build/tester \
  --target react_renderer_textlayoutmanager_tests react_renderer_attributedstring_tests -j 6
ctest --test-dir private/react-native-fantom/build/tester/react-common-tests \
  -R '^react/renderer/(textlayoutmanager|attributedstring)/tests/' --output-on-failure
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --rerun \
  --tests 'com.facebook.react.views.text.*' --console=plain --max-workers=4

The regression tests detect restoring epsilon equality, hashing raw NaNs, using raw shadow coordinates and omitting lineBreakMode from prepared equality. The C++ probe records the values returned by the cache and the number of entries erased on eviction.

Terminal output from the cache lookup and eviction probe

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 on the #60457 parent and on this branch. The size and line keys use the shared Fantom C++ types.

Key path Fragments Equality before → after Hash before → after
Size 1 550.05 → 541.14 370.05 → 413.06
Size 4 1741.17 → 1705.18 1108.15 → 1235.11
Lines 1 530.21 → 526.10 355.26 → 380.09
Lines 4 1753.29 → 1692.72 1083.79 → 1202.21
Android prepared 1 697.92 → 691.10 528.11 → 570.06
Android prepared 4 2332.55 → 2305.30 1731.30 → 1863.14
Android prepared with shadow 1 699.09 → 699.49 544.07 → 592.12
Android prepared with shadow 4 2340.25 → 2337.90 1795.19 → 1951.30

The repository's TextAttributesHashBenchmark.cpp fixtures retired 171.03–249.04 instructions per layout hash before and 189.49–265.06 after. Display hashes retired 324.05–495.07 before and 354.66–543.50 after.

ide added 2 commits December 2, 2026 01:13
Two Fabric paragraphs with the same source text can reuse a measurement even when they lay out differently. On iOS, an uppercase paragraph can reuse the lowercase paragraph's width and clip. On Android, `onTextLayout` can report the lowercase line while the view draws uppercase text.

Compare `textTransform`, `baseWritingDirection`, `lineBreakStrategy`, `lineBreakMode`, `layoutDirection`, and `textEffects` in both cache keys. These attributes can change the measured size, line frames, line text, or inline attachment positions. Keys that differ only in these fields share a hash and compare unequal, so each measurement gets a separate entry.

The oldest omission dates to the original size cache in 0.62. The line cache reused the same comparison when it shipped in 0.76.

Passed the Fantom ReactCommon text layout tests and manual RNTester checks on iOS 27.0 and Android 16. Each row of the table-driven regression fails for both keys when its comparison is removed. The benchmark measures equality for one- and four-fragment keys on Apple silicon.
Text measurement keys can compare equal and hash differently when their float values are close. On libc++, neighboring font sizes occupy separate entries even though the keys compare equal. An infinite font limit also prevents the LRU from erasing the corresponding map entry because the key does not compare equal to itself.

Compare key floats exactly and canonicalize NaN hashes, including the coordinates of a present shadow offset. Keep the existing attribute comparisons for renderer updates. Compare lineBreakMode in the prepared key because its display hash includes that field.

The Fantom text layout and attributed string suites cover adjacent floats, unset values, an infinite font limit, prepared shadow offsets and line break modes. The Android text unit tests pass.
@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
@ide
ide marked this pull request as ready for review December 2, 2026 12:20
@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

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

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