From 239b24a5e66234d582932900ff2648799d1ecb7e Mon Sep 17 00:00:00 2001 From: James Ide Date: Tue, 1 Dec 2026 22:40:57 -0800 Subject: [PATCH] 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. --- .../textlayoutmanager/TextMeasureCache.h | 15 ++++- .../tests/TextLayoutManagerTest.cpp | 59 ++++++++++++++++++- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h index 69cc621aaf75..c1eb566133d2 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/TextMeasureCache.h @@ -130,6 +130,11 @@ inline bool areTextAttributesEquivalentLayoutWise(const TextAttributes &lhs, con lhs.fontVariationSettings, lhs.allowFontScaling, lhs.dynamicTypeRamp, + lhs.textTransform, + lhs.baseWritingDirection, + lhs.lineBreakStrategy, + lhs.lineBreakMode, + lhs.layoutDirection, lhs.alignment) == std::tie( rhs.fontFamily, @@ -139,16 +144,20 @@ inline bool areTextAttributesEquivalentLayoutWise(const TextAttributes &lhs, con rhs.fontVariationSettings, rhs.allowFontScaling, rhs.dynamicTypeRamp, + rhs.textTransform, + rhs.baseWritingDirection, + rhs.lineBreakStrategy, + rhs.lineBreakMode, + rhs.layoutDirection, rhs.alignment) && floatEquality(lhs.fontSize, rhs.fontSize) && floatEquality(lhs.fontSizeMultiplier, rhs.fontSizeMultiplier) && floatEquality(lhs.letterSpacing, rhs.letterSpacing) && floatEquality(lhs.lineHeight, rhs.lineHeight) && - floatEquality(lhs.maxFontSizeMultiplier, rhs.maxFontSizeMultiplier); + floatEquality(lhs.maxFontSizeMultiplier, rhs.maxFontSizeMultiplier) && lhs.textEffects == rhs.textEffects; } inline size_t textAttributesHashLayoutWise(const TextAttributes &textAttributes) { - // Taking into account the same props as - // `areTextAttributesEquivalentLayoutWise` mentions. + // Hash a subset of the attributes compared by `areTextAttributesEquivalentLayoutWise`. // The optionals are folded into one presence mask rather than being chained individually, so // that the properties a given text run does not set stay off the hash's dependency chain. This // is the hot path: it runs on every measurement-cache probe. diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp index ac0593cd44c5..512a91e9ff69 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/tests/TextLayoutManagerTest.cpp @@ -82,7 +82,7 @@ TEST(TextLayoutManagerTest, emptyFontVariationSettingsClearInheritedSettings) { EXPECT_TRUE(parent.fontVariationSettings->empty()); } -TEST(TextLayoutManagerTest, everyLayoutAttributeAffectsEqualityAndHash) { +TEST(TextLayoutManagerTest, hashedLayoutAttributesAffectCacheKeys) { TextAttributes baseline; baseline.fontFamily = "Inter"; baseline.fontSize = 16; @@ -175,6 +175,63 @@ TEST(TextLayoutManagerTest, everyLayoutAttributeAffectsEqualityAndHash) { } } +TEST(TextLayoutManagerTest, additionalLayoutAttributesAffectCacheKeyEquality) { + AttributedString baseline; + baseline.appendFragment({.string = "Text"}); + + struct TestCase { + const char* name; + void (*mutate)(TextAttributes&); + }; + const std::array testCases{{ + {.name = "textTransform", + .mutate = + [](TextAttributes& attributes) { + attributes.textTransform = TextTransform::Uppercase; + }}, + {.name = "baseWritingDirection", + .mutate = + [](TextAttributes& attributes) { + attributes.baseWritingDirection = WritingDirection::RightToLeft; + }}, + {.name = "lineBreakStrategy", + .mutate = + [](TextAttributes& attributes) { + attributes.lineBreakStrategy = + LineBreakStrategy::HangulWordPriority; + }}, + {.name = "lineBreakMode", + .mutate = + [](TextAttributes& attributes) { + attributes.lineBreakMode = LineBreakMode::Char; + }}, + {.name = "layoutDirection", + .mutate = + [](TextAttributes& attributes) { + attributes.layoutDirection = LayoutDirection::RightToLeft; + }}, + {.name = "textEffects", + .mutate = + [](TextAttributes& attributes) { + attributes.textEffects.push_back( + {.name = "scale", .props = nullptr}); + }}, + }}; + + for (const auto& testCase : testCases) { + SCOPED_TRACE(testCase.name); + auto changed = baseline; + testCase.mutate(changed.getFragments().front().textAttributes); + + EXPECT_FALSE( + TextMeasureCacheKey{.attributedString = baseline} == + TextMeasureCacheKey{.attributedString = changed}); + EXPECT_FALSE( + LineMeasureCacheKey{.attributedString = baseline} == + LineMeasureCacheKey{.attributedString = changed}); + } +} + TEST(TextLayoutManagerTest, equivalentPopulatedLayoutAttributesHashEqually) { TextAttributes lhs; lhs.fontFamily = "Inter";