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
This commit is contained in:
Joshua Gross 2019-02-01 18:15:51 -08:00 committed by Facebook Github Bot
parent 1bbb69355c
commit 28b8b8e370
2 changed files with 37 additions and 12 deletions

View File

@ -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.
#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<std::shared_ptr<const ReactNativeConfig>>(
"ReactNativeConfig")
->getBool(paramName)
: false);
if (enableCache) {
measureCache_ = std::make_unique<ParagraphMeasurementCache>();
} 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.

View File

@ -61,22 +61,29 @@ 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
if (measureCache_ != nullptr) {
ParagraphMeasurementCacheKey cacheKey =
std::make_tuple(attributedString, attributes, layoutConstraints);
if (measureCache_->exists(cacheKey)) {
return measureCache_->get(cacheKey);
}
auto measuredSize = textLayoutManager_->measure(
attributedString, getProps()->paragraphAttributes, layoutConstraints);
auto measuredSize = makeMeasurements();
measureCache_->set(cacheKey, measuredSize);
return measuredSize;
}
return makeMeasurements();
}
void ParagraphShadowNode::layout(LayoutContext layoutContext) {
updateLocalDataIfNeeded();
ConcreteViewShadowNode::layout(layoutContext);