Repository navigation
Commit e1be1bf
authored
Compare layout-affecting attributes in text measurement cache keys
## 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.
```jsx
<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 |
| --- | --- |
|  |  |
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.
```sh
./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.
```sh
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.
```sh
./gradlew :packages:rn-tester:android:app:assembleDebug \
-PreactNativeArchitectures=arm64-v8a --console=plain --max-workers=4
```
| Android before | Android after |
| --- | --- |
|  |  |
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 |
Differential Revision: D135611163
Pull Request resolved: #604571 parent c25b8be commit e1be1bf
2 files changed
Lines changed: 70 additions & 4 deletions
File tree
- packages/react-native/ReactCommon/react/renderer/textlayoutmanager
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
133 | 138 | | |
134 | 139 | | |
135 | 140 | | |
| |||
139 | 144 | | |
140 | 145 | | |
141 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
142 | 152 | | |
143 | 153 | | |
144 | 154 | | |
145 | | - | |
| 155 | + | |
146 | 156 | | |
147 | 157 | | |
148 | 158 | | |
149 | 159 | | |
150 | | - | |
151 | | - | |
| 160 | + | |
152 | 161 | | |
153 | 162 | | |
154 | 163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
| 85 | + | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| |||
175 | 175 | | |
176 | 176 | | |
177 | 177 | | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
178 | 235 | | |
179 | 236 | | |
180 | 237 | | |
| |||
0 commit comments