From 2c28310267e68d3ac75615d27e22126202bd5411 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sun, 15 Jul 2018 16:46:24 -0700 Subject: [PATCH] Fabric: Computed attributed string caching inside Summary: @public Quite trivial. We had to have this from the day one. We don't need cache invalidation policy because all subtree is immutable. Reviewed By: mdvacca Differential Revision: D8709973 fbshipit-source-id: bd7fcf0ae1dcb23894321cb5d16da18cb1ab788f --- ReactCommon/fabric/attributedstring/AttributedString.h | 1 - ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp | 2 -- ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp | 7 ++++++- ReactCommon/fabric/text/paragraph/ParagraphShadowNode.h | 7 +++++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ReactCommon/fabric/attributedstring/AttributedString.h b/ReactCommon/fabric/attributedstring/AttributedString.h index 07cc47603e4..4fa5ac9b466 100644 --- a/ReactCommon/fabric/attributedstring/AttributedString.h +++ b/ReactCommon/fabric/attributedstring/AttributedString.h @@ -72,4 +72,3 @@ private: } // namespace react } // namespace facebook - diff --git a/ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp b/ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp index f2f2d08a7ee..ec97f412444 100644 --- a/ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp +++ b/ReactCommon/fabric/text/basetext/BaseTextShadowNode.cpp @@ -20,8 +20,6 @@ AttributedString BaseTextShadowNode::getAttributedString( const TextAttributes &textAttributes, const SharedShadowNodeSharedList &childNodes ) const { - // TODO: Implement caching. - AttributedString attributedString; for (const auto &childNode : *childNodes) { diff --git a/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp b/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp index e7b6513ba23..99222fc317f 100644 --- a/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp +++ b/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.cpp @@ -19,7 +19,12 @@ ComponentName ParagraphShadowNode::getComponentName() const { } AttributedString ParagraphShadowNode::getAttributedString() const { - return BaseTextShadowNode::getAttributedString(getProps()->textAttributes, getChildren()); + if (!cachedAttributedString_.has_value()) { + cachedAttributedString_ = + BaseTextShadowNode::getAttributedString(getProps()->textAttributes, getChildren()); + } + + return cachedAttributedString_.value(); } void ParagraphShadowNode::setTextLayoutManager(SharedTextLayoutManager textLayoutManager) { diff --git a/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.h b/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.h index df16aee1b79..f7b1ea4fd2a 100644 --- a/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.h +++ b/ReactCommon/fabric/text/paragraph/ParagraphShadowNode.h @@ -14,6 +14,7 @@ #include #include #include +#include namespace facebook { namespace react { @@ -63,6 +64,12 @@ private: void updateLocalData(); SharedTextLayoutManager textLayoutManager_; + + /* + * Cached attributed string that represents the content of the subtree started + * from the node. + */ + mutable folly::Optional cachedAttributedString_ {}; }; } // namespace react