Skip to content

Compare layout-affecting attributes in text measurement cache keys - #60457

Merged
meta-codesync[bot] merged 1 commit into
react:mainfrom
expo:@ide/text-measure-cache-key
Dec 2, 2026
Merged

meta-codesync[bot] merged 1 commit into
react:mainfrom
expo:@ide/text-measure-cache-key

Conversation

@ide

@ide ide commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary:

Two Fabric paragraphs with the same source text can reuse a measurement even when they lay out differently. On iOS, the second paragraph below can reuse the first paragraph's width of 165.7 pt, clipping the uppercase line. With the fix, the view measures 181.3 pt on the iPhone 18 Pro simulator. The screenshots show the larger RNTester fixture.

<View style={{alignItems: 'flex-start'}}>
  <Text style={{fontSize: 20, textTransform: 'none'}}>minimum minimum</Text>
  <Text style={{fontSize: 20, textTransform: 'uppercase'}}>minimum minimum</Text>
</View>
Before After
On main, the uppercase text clips and uses the lowercase line measurement With the fix, the uppercase text fits and has its own line measurement

Both cache keys now distinguish the six omitted fragment attributes textTransform, baseWritingDirection, lineBreakStrategy, lineBreakMode, layoutDirection, and textEffects. TextKit uses the transform, direction and paragraph breaking fields, which can change the size, line frames, line text, or inline attachment positions. Android measures the transformed string, uses the layout direction for alignment and permits custom text effect spans that change font metrics. Android's default prepared-layout path bypasses the size cache. The prepared key already compares the three Android fields. Fabric still calls the line cache for onTextLayout and baseline calculation.

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.

Keys that differ only in these fields share a hash and compare unequal, so each measurement gets a separate entry. The tests cover these six fields; they would not catch a field added to TextAttributes later and left out of the keys, which would take a check over the struct's declarations.

This change leaves two existing key defects alone: Android measures with the paragraph's base text attributes, which neither key includes, and the keys compare some floats with an epsilon but hash their exact values.

Changelog:

[GENERAL] [FIXED] - Avoid reusing Fabric text measurements across different transforms, directions, wrapping options, or Android text effects.

Test Plan:

Passed the Fantom ReactCommon text layout tests, covering equality for both cache keys, the layout hash and pixel-grid rounding. All six rows of the table-driven regression failed on main. Removing one comparison at a time made only its corresponding row fail for both keys.

./gradlew :private:react-native-fantom:configureFantomTester --console=plain --max-workers=6
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

Passed a manual RNTester check on an iPhone 18 Pro with iOS 27.0. I used the same local screen in both builds and displayed onLayout and onTextLayout values alongside the text. The uppercase view gets enough width for both words, and hangul-word gets enough height for the third word. Two separately mounted multiline inputs verify that the character-wrapped input gets the height of its three lines. The inline attachment moves into the gap in the RTL writing-direction sample, and the right-aligned direction sample reports its own line x coordinate.

xcodebuild -workspace packages/rn-tester/RNTesterPods.xcworkspace -scheme RNTester \
  -configuration Debug -destination 'generic/platform=iOS Simulator' \
  -derivedDataPath /private/tmp/rn-measure-cache-derived \
  -jobs 6 ARCHS=arm64 ONLY_ACTIVE_ARCH=YES CODE_SIGNING_ALLOWED=NO build

Passed a manual RNTester check on an Android 16 arm64 emulator. The local screen pairs each fixed-size sample with a matching size, source string and font style. The effect sample uses a local CacheProbeScale factory that returns a ScaleXSpan(1.6f) implementing TextEffectSpan. At the same view sizes, onTextLayout reported the uppercase string, the scaled line width, and the right-aligned line's x coordinate.

./gradlew :packages:rn-tester:android:app:assembleDebug \
  -PreactNativeArchitectures=arm64-v8a --console=plain --max-workers=4
Android before Android after
Android line measurements for the fixed-width uppercase, scaled and right-aligned samples match the first samples Android reports the uppercase line, the scaled width and the right-aligned x coordinate

The layout hash uses the same code as main. I measured equality on an Apple silicon Mac (Apple M6) with independent, equal keys whose fragments have no attachments or text effects. Apple Clang compiled the benchmark, AttributedString.cpp, and ParagraphAttributes.cpp at -O3. The table reports median retired instructions per comparison, including loop overhead, from five repetitions of 10 million comparisons after warming each executable.

Key Fragments Main Patched Delta
TextMeasureCacheKey 1 456.05 550.08 +94.03
TextMeasureCacheKey 4 1365.13 1741.22 +376.09
LineMeasureCacheKey 1 436.05 530.03 +93.98
LineMeasureCacheKey 4 1345.24 1721.14 +375.90

@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
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.
@ide
ide force-pushed the @ide/text-measure-cache-key branch from 92f05b0 to 239b24a Compare December 2, 2026 09:13
@ide
ide marked this pull request as ready for review December 2, 2026 10:44
@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
@meta-codesync

meta-codesync Bot commented Dec 2, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D135611163.

@christophpurrer christophpurrer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync
meta-codesync Bot merged commit e1be1bf into react:main Dec 2, 2026
105 of 106 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @ide in e1be1bf

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label 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. Merged This PR has been merged. 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.

4 participants