From e737535f18cb837e0ffda4e7a703227a3f89a418 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Thu, 13 Feb 2020 11:37:55 -0800 Subject: [PATCH] Fabric: Small optimization in RCTNSAttributedStringFromAttributedString Summary: With new logic we save one attributed string copy operation. Changelog: [Internal] Fabric-specific internal change. Reviewed By: sammy-SC Differential Revision: D19844371 fbshipit-source-id: 62f6b0f9c8514a1b55224ccaa52c8fa89c06c9e7 --- .../platform/ios/RCTAttributedTextUtils.mm | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm index 17149b70ad2..4bc9870a628 100644 --- a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm +++ b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm @@ -228,7 +228,7 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt [nsAttributedString beginEditing]; for (auto fragment : attributedString.getFragments()) { - NSAttributedString *nsAttributedStringFragment; + NSMutableAttributedString *nsAttributedStringFragment; if (fragment.isAttachment()) { auto layoutMetrics = fragment.parentShadowView.layoutMetrics; @@ -239,18 +239,15 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt attachment.image = placeholderImage; attachment.bounds = bounds; - nsAttributedStringFragment = [NSAttributedString attributedStringWithAttachment:attachment]; + nsAttributedStringFragment = [[NSMutableAttributedString attributedStringWithAttachment:attachment] mutableCopy]; } else { NSString *string = [NSString stringWithCString:fragment.string.c_str() encoding:NSUTF8StringEncoding]; - nsAttributedStringFragment = - [[NSAttributedString alloc] initWithString:string - attributes:RCTNSTextAttributesFromTextAttributes(fragment.textAttributes)]; + nsAttributedStringFragment = [[NSMutableAttributedString alloc] + initWithString:string + attributes:RCTNSTextAttributesFromTextAttributes(fragment.textAttributes)]; } - NSMutableAttributedString *nsMutableAttributedStringFragment = - [[NSMutableAttributedString alloc] initWithAttributedString:nsAttributedStringFragment]; - if (fragment.parentShadowView.componentHandle) { RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new]; eventEmitterWrapper.eventEmitter = fragment.parentShadowView.eventEmitter; @@ -258,11 +255,11 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt NSDictionary *additionalTextAttributes = @{RCTAttributedStringEventEmitterKey : eventEmitterWrapper}; - [nsMutableAttributedStringFragment addAttributes:additionalTextAttributes - range:NSMakeRange(0, nsMutableAttributedStringFragment.length)]; + [nsAttributedStringFragment addAttributes:additionalTextAttributes + range:NSMakeRange(0, nsAttributedStringFragment.length)]; } - [nsAttributedString appendAttributedString:nsMutableAttributedStringFragment]; + [nsAttributedString appendAttributedString:nsAttributedStringFragment]; } [nsAttributedString endEditing];