From 7fc58f2192f1920bea56f448a14e12bc7de0c980 Mon Sep 17 00:00:00 2001 From: Gregory Moskaliuk Date: Mon, 19 Oct 2026 10:51:38 -0700 Subject: [PATCH] Avoid copying text fragments and re-attributing runs when building NSAttributedString (#59154) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: `RCTNSAttributedStringFromAttributedString` (`ReactCommon/react/renderer/textlayoutmanager/platform/ios/.../RCTAttributedTextUtils.mm`) converts a paragraph's `AttributedString` into the `NSAttributedString` TextKit lays out and draws. It runs once per paragraph per measurement when the attributed-string cache misses, so for a screen of distinct labels it runs once per label. **Current behavior.** Two pieces of redundant work per fragment: - the loop iterates `getFragments()` by value (`for (auto fragment : ...)`), copying each `Fragment`: its `std::string`, its `TextAttributes`, and a `ShadowView` with three `shared_ptr` members (props, event emitter, state), so three atomic retain/release pairs per fragment; - the event emitter is attached in a second `addAttributes:range:` pass over the fragment's string right after it was created with its attributes, which splits and re-merges the attribute runs TextKit just built. For an attachment, the string from `attributedStringWithAttachment:` is also copied with `mutableCopy` before that pass. **With this change.** Fragments are iterated by reference, and `RCTNSAttributedStringFragmentFromFragment` creates each fragment's string with all of its attributes at once, including the event emitter, for text and attachment fragments alike. The second `addAttributes:range:` pass and the wrapper function that ran it are gone. The attachment string is created with `initWithString:attributes:` from `NSAttachmentCharacter` and `NSAttachmentAttributeName`, which is what `attributedStringWithAttachment:` produces, so it needs no copy. Every run ends up with the same attributes as before. ## Changelog: [IOS] [CHANGED] - Build a paragraph's NSAttributedString without copying each text fragment or re-attributing its runs Pull Request resolved: https://github.com/react/react-native/pull/59154 Test Plan: - `node scripts/clang-format.js --check` on the changed file passes. - RNTester built with `xcodebuild` in Release and run on a physical iPhone 15 Pro (iOS 26.6.1). - **Same output.** A local build ran `main`'s conversion next to this one for every paragraph and compared the two results run by run: string, run ranges and every attribute, with attachments compared by bounds and image and event emitters by the emitter they wrap. Scrolling through the RNTester Text examples, which include inline views and pressable nested text, and mounting 1000 labels five times in the Playground compared over 31,000 conversions, with 217 attachment fragments and over 32,000 fragments carrying an event emitter. There were no differences, and the same check reports an emitter removed on purpose. - **Measurement.** In the same build, both conversions were timed on the same input, alternating which ran first. Mounting 1000 system-font labels five times in the RNTester Playground (27,750 conversions): 135.3 ms with `main`'s conversion and 99.4 ms with this change, −26% of the conversion step. The presses were driven by [Argent](https://github.com/software-mansion/argent) on the physical iPhone. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed By: cipolleschi Differential Revision: D126056330 Pulled By: javache fbshipit-source-id: 620dfa63b57e905f98ed57a1e304d14db9915e72 --- .../RCTAttributedTextUtils.mm | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm index 95ff5a0d180..b988908b6fd 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm @@ -405,6 +405,9 @@ static NSMutableAttributedString *RCTNSAttributedStringFragmentFromFragment( const AttributedString::Fragment &fragment, UIImage *placeholderImage) { + NSString *string; + NSMutableDictionary *attributes; + if (fragment.isAttachment()) { auto layoutMetrics = fragment.parentShadowView.layoutMetrics; CGRect bounds = { @@ -415,41 +418,28 @@ static NSMutableAttributedString *RCTNSAttributedStringFragmentFromFragment( attachment.image = placeholderImage; attachment.bounds = bounds; - return [[NSMutableAttributedString attributedStringWithAttachment:attachment] mutableCopy]; + string = [NSString stringWithFormat:@"%C", (unichar)NSAttachmentCharacter]; + attributes = [NSMutableDictionary dictionaryWithObject:attachment forKey:NSAttachmentAttributeName]; } else { NSString *decoded = [[NSString alloc] initWithBytes:fragment.string.data() length:fragment.string.size() encoding:NSUTF8StringEncoding]; - NSString *string = decoded != nil ? decoded : @""; + string = decoded != nil ? decoded : @""; if (fragment.textAttributes.textTransform.has_value()) { auto textTransform = fragment.textAttributes.textTransform.value(); string = RCTNSStringFromStringApplyingTextTransform(string, textTransform); } - return [[NSMutableAttributedString alloc] - initWithString:string - attributes:RCTNSTextAttributesFromTextAttributes(fragment.textAttributes)]; + attributes = RCTNSTextAttributesFromTextAttributes(fragment.textAttributes); } -} - -static NSMutableAttributedString *RCTNSAttributedStringFragmentWithAttributesFromFragment( - const AttributedString::Fragment &fragment, - UIImage *placeholderImage) -{ - auto nsAttributedStringFragment = RCTNSAttributedStringFragmentFromFragment(fragment, placeholderImage); + // Set with the initial attributes; adding it afterwards would re-split the runs. if (fragment.parentShadowView.componentHandle) { - auto eventEmitterWrapper = RCTWrapEventEmitter(fragment.parentShadowView.eventEmitter); - - NSDictionary *additionalTextAttributes = - @{RCTAttributedStringEventEmitterKey : eventEmitterWrapper}; - - [nsAttributedStringFragment addAttributes:additionalTextAttributes - range:NSMakeRange(0, nsAttributedStringFragment.length)]; + attributes[RCTAttributedStringEventEmitterKey] = RCTWrapEventEmitter(fragment.parentShadowView.eventEmitter); } - return nsAttributedStringFragment; + return [[NSMutableAttributedString alloc] initWithString:string attributes:attributes]; } NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedString &attributedString) @@ -464,9 +454,9 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt [nsAttributedString beginEditing]; - for (auto fragment : attributedString.getFragments()) { + for (const auto &fragment : attributedString.getFragments()) { NSMutableAttributedString *nsAttributedStringFragment = - RCTNSAttributedStringFragmentWithAttributesFromFragment(fragment, placeholderImage); + RCTNSAttributedStringFragmentFromFragment(fragment, placeholderImage); [nsAttributedString appendAttributedString:nsAttributedStringFragment]; }