Repository navigation
Share iOS text measurements that differ only in unused constraints - #59527
intergalacticspacehighway merged 3 commits into
Conversation
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 <noreply@anthropic.com>
Visual regressionNo visual differences at android: no capture |
|
I checked the iOS screenshot for |
|
@javache could you apply |
intergalacticspacehighway
left a comment
There was a problem hiding this comment.
This looks great, thanks!
We can tighten this up a bit:
- Could you pass
keyConstraintstomeasureAttributedString:in the cache-miss lambda? Same behavior today, but if the measure ever starts readingminimumSizeormaximumSize.height, all callers that share an entry still get the same result. - Maybe say "minimum size and maximum height are left out of the key" in the comment, since
layoutDirectionstays in the key. A one-line note atRCTTextLayoutManager.mm:69about this would also help future edits. - Optional: a small test would be nice. Set a
TransactionTelemetryas thread-local, then check withgetNumberOfTextMeasurements()that calls that differ only in min size or max height hit the cache (and still get clamped), and that a different max width misses. #60169 addsRCTTextLayoutManagertests, so it could go there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Gregory Moskaliuk <gregory.moskaliuk@swmansion.com>
|
@intergalacticspacehighway Thanks! All three are done, and I merged the latest
Still −28% on an iPhone 15 Pro (Release, 1,000 labels): mount 145 → 105 ms, update 145 → 104 ms. |
intergalacticspacehighway
left a comment
There was a problem hiding this comment.
LGTM, thanks for the updates and the test!
Summary:
On iOS,
TextLayoutManager::measurelooks up each Text measurement inTextMeasureCache, keyed by the attributed string, the paragraph attributes and the fullLayoutConstraints.Current behavior. The native measurement (
-[RCTTextLayoutManager measureNSAttributedString:…]) only reads the maximum width: it lays the text out in a container of{maximumSize.width, CGFLOAT_MAX}. The minimum size and the maximum height don't change the result, andmeasureclamps the result to the full constraints after the cache. But they are part of the cache key, so measurements that differ only in them miss the cache and lay the same text out again with TextKit.This happens when Yoga measures a Text more than once in a layout pass. For example, a Text in a wrapping row is measured twice with an at-most width and once with an exact width. The two at-most measurements have the same maximum width but different constraints otherwise, so both miss for new text.
With this change. The cache key leaves out the minimum size and the maximum height. Measurements that only differ in them share an entry. The native measurement still receives the original constraints, and the clamp after the cache still uses them, so every caller gets the same size as before.
Android already ignores the maximum height for the same reason (
platform/android/.../TextLayoutManager.cpp: "We assume max height will have no effect on measurement, so we override it … to enable cache reuse"). On iOS the minimum size can be left out too, because the native measurement never reads it.Changelog:
[IOS] [CHANGED] - Text measurements that differ only in the minimum size or maximum height share a cache entry
Test Plan:
xcodebuildin Release and run on a physical iPhone 15 Pro (iOS 26.6.1), frommainat 4420343.<Text>labels in a wrapping row (flexDirection: 'row', flexWrap: 'wrap'), 10 rounds per run, and updating the text of the same 1,000 labels 30 times per run. Time fromsetStateto the layout effect, which includes React rendering, layout and text measurement. "Before" and the first "this PR" runs are the same binary with the cache key switched by a launch argument (local only, not part of this PR); the last "this PR" run of each row is a build of exactly this change. The presses were driven by Argent on the physical iPhone. Medians per run:clang-format, which needsdotslash.🤖 Generated with Claude Code