Skip to content

Commit e1be1bf

Browse files
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 | | --- | --- | | ![On main, the uppercase text clips and uses the lowercase line measurement](https://raw.githubusercontent.com/expo/react-native/016c19d6889ad138f80480a98019f0abb7778a26/text-measure-cache-key/text-measure-cache-ios-before.png) | ![With the fix, the uppercase text fits and has its own line measurement](https://raw.githubusercontent.com/expo/react-native/016c19d6889ad138f80480a98019f0abb7778a26/text-measure-cache-key/text-measure-cache-ios-after.png) | 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 | | --- | --- | | ![Android line measurements for the fixed-width uppercase, scaled and right-aligned samples match the first samples](https://raw.githubusercontent.com/expo/react-native/016c19d6889ad138f80480a98019f0abb7778a26/text-measure-cache-key/text-measure-cache-android-before.png) | ![Android reports the uppercase line, the scaled width and the right-aligned x coordinate](https://raw.githubusercontent.com/expo/react-native/016c19d6889ad138f80480a98019f0abb7778a26/text-measure-cache-key/text-measure-cache-android-after.png) | 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: #60457
1 parent c25b8be commit e1be1bf

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ inline bool areTextAttributesEquivalentLayoutWise(const TextAttributes &lhs, con
130130
lhs.fontVariationSettings,
131131
lhs.allowFontScaling,
132132
lhs.dynamicTypeRamp,
133+
lhs.textTransform,
134+
lhs.baseWritingDirection,
135+
lhs.lineBreakStrategy,
136+
lhs.lineBreakMode,
137+
lhs.layoutDirection,
133138
lhs.alignment) ==
134139
std::tie(
135140
rhs.fontFamily,
@@ -139,16 +144,20 @@ inline bool areTextAttributesEquivalentLayoutWise(const TextAttributes &lhs, con
139144
rhs.fontVariationSettings,
140145
rhs.allowFontScaling,
141146
rhs.dynamicTypeRamp,
147+
rhs.textTransform,
148+
rhs.baseWritingDirection,
149+
rhs.lineBreakStrategy,
150+
rhs.lineBreakMode,
151+
rhs.layoutDirection,
142152
rhs.alignment) &&
143153
floatEquality(lhs.fontSize, rhs.fontSize) && floatEquality(lhs.fontSizeMultiplier, rhs.fontSizeMultiplier) &&
144154
floatEquality(lhs.letterSpacing, rhs.letterSpacing) && floatEquality(lhs.lineHeight, rhs.lineHeight) &&
145-
floatEquality(lhs.maxFontSizeMultiplier, rhs.maxFontSizeMultiplier);
155+
floatEquality(lhs.maxFontSizeMultiplier, rhs.maxFontSizeMultiplier) && lhs.textEffects == rhs.textEffects;
146156
}
147157

148158
inline size_t textAttributesHashLayoutWise(const TextAttributes &textAttributes)
149159
{
150-
// Taking into account the same props as
151-
// `areTextAttributesEquivalentLayoutWise` mentions.
160+
// Hash a subset of the attributes compared by `areTextAttributesEquivalentLayoutWise`.
152161
// The optionals are folded into one presence mask rather than being chained individually, so
153162
// that the properties a given text run does not set stay off the hash's dependency chain. This
154163
// is the hot path: it runs on every measurement-cache probe.

‎packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ TEST(TextLayoutManagerTest, emptyFontVariationSettingsClearInheritedSettings) {
8282
EXPECT_TRUE(parent.fontVariationSettings->empty());
8383
}
8484

85-
TEST(TextLayoutManagerTest, everyLayoutAttributeAffectsEqualityAndHash) {
85+
TEST(TextLayoutManagerTest, hashedLayoutAttributesAffectCacheKeys) {
8686
TextAttributes baseline;
8787
baseline.fontFamily = "Inter";
8888
baseline.fontSize = 16;
@@ -175,6 +175,63 @@ TEST(TextLayoutManagerTest, everyLayoutAttributeAffectsEqualityAndHash) {
175175
}
176176
}
177177

178+
TEST(TextLayoutManagerTest, additionalLayoutAttributesAffectCacheKeyEquality) {
179+
AttributedString baseline;
180+
baseline.appendFragment({.string = "Text"});
181+
182+
struct TestCase {
183+
const char* name;
184+
void (*mutate)(TextAttributes&);
185+
};
186+
const std::array<TestCase, 6> testCases{{
187+
{.name = "textTransform",
188+
.mutate =
189+
[](TextAttributes& attributes) {
190+
attributes.textTransform = TextTransform::Uppercase;
191+
}},
192+
{.name = "baseWritingDirection",
193+
.mutate =
194+
[](TextAttributes& attributes) {
195+
attributes.baseWritingDirection = WritingDirection::RightToLeft;
196+
}},
197+
{.name = "lineBreakStrategy",
198+
.mutate =
199+
[](TextAttributes& attributes) {
200+
attributes.lineBreakStrategy =
201+
LineBreakStrategy::HangulWordPriority;
202+
}},
203+
{.name = "lineBreakMode",
204+
.mutate =
205+
[](TextAttributes& attributes) {
206+
attributes.lineBreakMode = LineBreakMode::Char;
207+
}},
208+
{.name = "layoutDirection",
209+
.mutate =
210+
[](TextAttributes& attributes) {
211+
attributes.layoutDirection = LayoutDirection::RightToLeft;
212+
}},
213+
{.name = "textEffects",
214+
.mutate =
215+
[](TextAttributes& attributes) {
216+
attributes.textEffects.push_back(
217+
{.name = "scale", .props = nullptr});
218+
}},
219+
}};
220+
221+
for (const auto& testCase : testCases) {
222+
SCOPED_TRACE(testCase.name);
223+
auto changed = baseline;
224+
testCase.mutate(changed.getFragments().front().textAttributes);
225+
226+
EXPECT_FALSE(
227+
TextMeasureCacheKey{.attributedString = baseline} ==
228+
TextMeasureCacheKey{.attributedString = changed});
229+
EXPECT_FALSE(
230+
LineMeasureCacheKey{.attributedString = baseline} ==
231+
LineMeasureCacheKey{.attributedString = changed});
232+
}
233+
}
234+
178235
TEST(TextLayoutManagerTest, equivalentPopulatedLayoutAttributesHashEqually) {
179236
TextAttributes lhs;
180237
lhs.fontFamily = "Inter";

0 commit comments

Comments
 (0)