From abd34560817ac33962b1152f2e8f2d28fe6e8892 Mon Sep 17 00:00:00 2001 From: Gregory Moskaliuk Date: Tue, 27 Oct 2026 11:21:29 +0100 Subject: [PATCH 1/2] Share iOS text measurements that differ only in unused constraints The native iOS measurement depends only on the maximum width, and the result is clamped to the full constraints after the cache, so the minimum size and the maximum height are left out of the cache key. Co-Authored-By: Claude Opus 5.5 --- .../react/renderer/textlayoutmanager/TextLayoutManager.mm | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm index 0f2fbe82b8f5..09ad2c43f9e5 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm @@ -8,6 +8,7 @@ #import "TextLayoutManager.h" #import "RCTTextLayoutManager.h" +#import #import #import @@ -53,10 +54,15 @@ const auto &attributedString = placeholderAttributedString ? *placeholderAttributedString : originalAttributedString; + // The native measurement depends only on the maximum width, and the result is clamped to the + // full constraints below, outside the cache, so the rest is left out of the key. + LayoutConstraints keyConstraints = layoutConstraints; + keyConstraints.minimumSize = Size{}; + keyConstraints.maximumSize.height = std::numeric_limits::infinity(); measurement = textMeasureCache_.get( {.attributedString = attributedString, .paragraphAttributes = paragraphAttributes, - .layoutConstraints = layoutConstraints, + .layoutConstraints = keyConstraints, .pointScaleFactor = layoutContext.pointScaleFactor}, [&]() { auto telemetry = TransactionTelemetry::threadLocalTelemetry(); From 06b733e34a5779cf4172105ec7950fb02bc9f918 Mon Sep 17 00:00:00 2001 From: Gregory Moskaliuk Date: Sat, 5 Dec 2026 10:42:40 +0100 Subject: [PATCH 2/2] Measure text with the cache key's constraints and test the cache key Co-Authored-By: Claude Opus 5.5 Signed-off-by: Gregory Moskaliuk --- .../Tests/Text/RCTTextMeasureCacheTests.mm | 119 ++++++++++++++++++ .../textlayoutmanager/RCTTextLayoutManager.mm | 2 + .../textlayoutmanager/TextLayoutManager.mm | 6 +- 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 packages/react-native/React/Tests/Text/RCTTextMeasureCacheTests.mm diff --git a/packages/react-native/React/Tests/Text/RCTTextMeasureCacheTests.mm b/packages/react-native/React/Tests/Text/RCTTextMeasureCacheTests.mm new file mode 100644 index 000000000000..7cfde60bd51f --- /dev/null +++ b/packages/react-native/React/Tests/Text/RCTTextMeasureCacheTests.mm @@ -0,0 +1,119 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace facebook::react; + +@interface RCTTextMeasureCacheTests : XCTestCase + +@end + +@implementation RCTTextMeasureCacheTests { + std::unique_ptr _textLayoutManager; + std::unique_ptr _telemetry; +} + +- (void)setUp +{ + [super setUp]; + _textLayoutManager = std::make_unique(std::make_shared()); + _telemetry = std::make_unique(); + _telemetry->setAsThreadLocal(); +} + +- (void)tearDown +{ + _telemetry->unsetAsThreadLocal(); + _telemetry.reset(); + _textLayoutManager.reset(); + [super tearDown]; +} + +static AttributedStringBox AttributedStringBoxWithText(std::string text) +{ + auto attributedString = AttributedString{}; + attributedString.appendFragment( + {.string = std::move(text), .textAttributes = TextAttributes::defaultTextAttributes()}); + return AttributedStringBox{attributedString}; +} + +static LayoutConstraints ConstraintsWithMaximumWidth(Float maximumWidth) +{ + auto constraints = LayoutConstraints{}; + constraints.maximumSize.width = maximumWidth; + return constraints; +} + +- (TextMeasurement)measure:(const AttributedStringBox &)attributedStringBox + constraints:(const LayoutConstraints &)constraints +{ + return _textLayoutManager->measure(attributedStringBox, ParagraphAttributes{}, TextLayoutContext{}, constraints); +} + +- (void)testConstraintsThatDifferOnlyInMinimumSizeShareAMeasurement +{ + auto text = AttributedStringBoxWithText("Hello, world"); + + auto constraints = ConstraintsWithMaximumWidth(200); + auto measurement = [self measure:text constraints:constraints]; + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + + auto withMinimumSize = constraints; + withMinimumSize.minimumSize = facebook::react::Size{.width = 150, .height = 60}; + auto measurementWithMinimumSize = [self measure:text constraints:withMinimumSize]; + + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + // The shared measurement is still clamped to the caller's constraints. + XCTAssertLessThan(measurement.size.width, 150); + XCTAssertEqual(measurementWithMinimumSize.size.width, 150); + XCTAssertEqual(measurementWithMinimumSize.size.height, 60); +} + +- (void)testConstraintsThatDifferOnlyInMaximumHeightShareAMeasurement +{ + auto text = AttributedStringBoxWithText("Hello, world"); + + auto constraints = ConstraintsWithMaximumWidth(200); + auto measurement = [self measure:text constraints:constraints]; + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + XCTAssertGreaterThan(measurement.size.height, 5); + + auto withMaximumHeight = constraints; + withMaximumHeight.maximumSize.height = 5; + auto measurementWithMaximumHeight = [self measure:text constraints:withMaximumHeight]; + + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + // The shared measurement is still clamped to the caller's constraints. + XCTAssertEqual(measurementWithMaximumHeight.size.height, 5); + XCTAssertEqual(measurementWithMaximumHeight.size.width, measurement.size.width); +} + +- (void)testConstraintsWithADifferentMaximumWidthMeasureAgain +{ + auto text = AttributedStringBoxWithText("Hello, world"); + + [self measure:text constraints:ConstraintsWithMaximumWidth(200)]; + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + + [self measure:text constraints:ConstraintsWithMaximumWidth(30)]; + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 2); +} + +@end diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm index 83e63854cd14..56990e739dcd 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm @@ -66,6 +66,8 @@ - (TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedStr return {}; } + // Only the maximum width is read here. TextLayoutManager::measure relies on that: its cache key leaves out the + // minimum size and the maximum height. CGSize maximumSize = CGSize{layoutConstraints.maximumSize.width, CGFLOAT_MAX}; NSTextStorage *textStorage = [self _textStorageAndLayoutManagerWithAttributesString:attributedString paragraphAttributes:paragraphAttributes diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm index 09ad2c43f9e5..5a326011bd31 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm @@ -55,7 +55,9 @@ placeholderAttributedString ? *placeholderAttributedString : originalAttributedString; // The native measurement depends only on the maximum width, and the result is clamped to the - // full constraints below, outside the cache, so the rest is left out of the key. + // full constraints below, outside the cache. So the minimum size and the maximum height are left + // out of the key, and the measurement gets the key's constraints, so that every caller sharing + // an entry gets the same result. LayoutConstraints keyConstraints = layoutConstraints; keyConstraints.minimumSize = Size{}; keyConstraints.maximumSize.height = std::numeric_limits::infinity(); @@ -73,7 +75,7 @@ auto measurement = [textLayoutManager measureAttributedString:attributedString paragraphAttributes:paragraphAttributes layoutContext:layoutContext - layoutConstraints:layoutConstraints]; + layoutConstraints:keyConstraints]; if (telemetry) { telemetry->didMeasureText();