diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphMeasurementCache.h b/ReactCommon/fabric/components/text/paragraph/ParagraphMeasurementCache.h index 6c4f2a8905..68dcb68631 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphMeasurementCache.h +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphMeasurementCache.h @@ -7,47 +7,21 @@ #pragma once -#include - #include #include #include +#include namespace facebook { namespace react { + using ParagraphMeasurementCacheKey = std::tuple; using ParagraphMeasurementCacheValue = Size; - -class ParagraphMeasurementCache { - public: - ParagraphMeasurementCache() : cache_{256} {} - - bool exists(const ParagraphMeasurementCacheKey &key) const { - std::lock_guard lock(mutex_); - return cache_.exists(key); - } - - ParagraphMeasurementCacheValue get( - const ParagraphMeasurementCacheKey &key) const { - std::lock_guard lock(mutex_); - return cache_.get(key); - } - - void set( - const ParagraphMeasurementCacheKey &key, - const ParagraphMeasurementCacheValue &value) const { - std::lock_guard lock(mutex_); - cache_.set(key, value); - } - - private: - mutable folly::EvictingCacheMap< - ParagraphMeasurementCacheKey, - ParagraphMeasurementCacheValue> - cache_; - mutable std::mutex mutex_; -}; +using ParagraphMeasurementCache = SimpleThreadSafeCache< + ParagraphMeasurementCacheKey, + ParagraphMeasurementCacheValue, + 256>; } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp index 68def552b0..13d80f4e9a 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp @@ -59,29 +59,22 @@ void ParagraphShadowNode::updateLocalDataIfNeeded() { Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const { AttributedString attributedString = getAttributedString(); - const ParagraphAttributes attributes = getProps()->paragraphAttributes; - - auto makeMeasurements = [&] { - return textLayoutManager_->measure( - attributedString, getProps()->paragraphAttributes, layoutConstraints); - }; + const ParagraphAttributes paragraphAttributes = + getProps()->paragraphAttributes; // Cache results of this function so we don't need to call measure() - // repeatedly - if (measureCache_ != nullptr) { - ParagraphMeasurementCacheKey cacheKey = - std::make_tuple(attributedString, attributes, layoutConstraints); - if (measureCache_->exists(cacheKey)) { - return measureCache_->get(cacheKey); - } - - auto measuredSize = makeMeasurements(); - measureCache_->set(cacheKey, measuredSize); - - return measuredSize; + // repeatedly. + if (measureCache_) { + return measureCache_->get( + ParagraphMeasurementCacheKey{attributedString, paragraphAttributes, layoutConstraints}, + [&](const ParagraphMeasurementCacheKey &key) { + return textLayoutManager_->measure( + attributedString, paragraphAttributes, layoutConstraints); + }); } - return makeMeasurements(); + return textLayoutManager_->measure( + attributedString, paragraphAttributes, layoutConstraints); } void ParagraphShadowNode::layout(LayoutContext layoutContext) {