Skip to content

Commit 92f05b0

Browse files
committed
Compare layout-affecting attributes in text measurement cache keys
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.
1 parent f7f1ab5 commit 92f05b0

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)