From 28b8b8e3701a0e92bcad3b2d77bf6008bc498427 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Fri, 1 Feb 2019 18:15:51 -0800 Subject: [PATCH] Use feature flags in Fabric C++ core, in particular for paragraph measurement caching Summary: The goal is to be able to use MobileConfig params inside of core React Native C++ code. This works on Catalyst iOS now and Wilde; need to add support for FB4A and Catalyst Android. Reviewed By: fkgozali Differential Revision: D13883007 fbshipit-source-id: 115fe6cc884d2a0b9ca26dadf867a5f0ae99f262 --- .../paragraph/ParagraphComponentDescriptor.h | 22 +++++++++++++-- .../text/paragraph/ParagraphShadowNode.cpp | 27 ++++++++++++------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphComponentDescriptor.h b/ReactCommon/fabric/components/text/paragraph/ParagraphComponentDescriptor.h index 12d5eee28..f87fffdad 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphComponentDescriptor.h +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphComponentDescriptor.h @@ -34,7 +34,24 @@ class ParagraphComponentDescriptor final // Every single `ParagraphShadowNode` will have a reference to // a shared `EvictingCacheMap`, a simple LRU cache for Paragraph // measurements. - measureCache_ = std::make_unique(); +#ifdef ANDROID + auto paramName = "react_fabric:enabled_paragraph_measure_cache_android"; +#else + auto paramName = "react_fabric:enabled_paragraph_measure_cache_ios"; +#endif + // TODO: T39927960 - get rid of this if statement + bool enableCache = + (contextContainer != nullptr + ? contextContainer + ->getInstance>( + "ReactNativeConfig") + ->getBool(paramName) + : false); + if (enableCache) { + measureCache_ = std::make_unique(); + } else { + measureCache_ = nullptr; + } } void adopt(UnsharedShadowNode shadowNode) const override { @@ -50,7 +67,8 @@ class ParagraphComponentDescriptor final // `ParagraphShadowNode` uses this to cache the results of text rendering // measurements. - paragraphShadowNode->setMeasureCache(measureCache_.get()); + paragraphShadowNode->setMeasureCache( + measureCache_ ? measureCache_.get() : nullptr); // All `ParagraphShadowNode`s must have leaf Yoga nodes with properly // setup measure function. diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp index c9d5270ff..68def552b 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp @@ -61,20 +61,27 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const { AttributedString attributedString = getAttributedString(); const ParagraphAttributes attributes = getProps()->paragraphAttributes; + auto makeMeasurements = [&] { + return textLayoutManager_->measure( + attributedString, getProps()->paragraphAttributes, layoutConstraints); + }; + // Cache results of this function so we don't need to call measure() // repeatedly - ParagraphMeasurementCacheKey cacheKey = - std::make_tuple(attributedString, attributes, layoutConstraints); - if (measureCache_->exists(cacheKey)) { - return measureCache_->get(cacheKey); + 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; } - auto measuredSize = textLayoutManager_->measure( - attributedString, getProps()->paragraphAttributes, layoutConstraints); - - measureCache_->set(cacheKey, measuredSize); - - return measuredSize; + return makeMeasurements(); } void ParagraphShadowNode::layout(LayoutContext layoutContext) {