mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Fabric: Moving MeasureCache into TextLayoutManager
Summary: We will have several consumers for the measure infra soon (TextInput will use the same TextLayoutManager). It makes sense to move the cache there. In the future, iOS and Android implementations will probably use a bit different (platform-specific) cache implementations because we will implement the ability to measure "opaque"/platform-specific text containers alongside with normal AttributeStrings. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D18445855 fbshipit-source-id: 7b7a65152ac13c74525da695612ae034904e82bf
This commit is contained in:
committed by
Facebook Github Bot
parent
5f0435fb85
commit
f32aa2a516
@@ -7,10 +7,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ParagraphMeasurementCache.h"
|
||||
#include "ParagraphShadowNode.h"
|
||||
|
||||
#include <folly/container/EvictingCacheMap.h>
|
||||
#include <react/config/ReactNativeConfig.h>
|
||||
#include <react/core/ConcreteComponentDescriptor.h>
|
||||
#include <react/textlayoutmanager/TextLayoutManager.h>
|
||||
@@ -36,10 +34,6 @@ class ParagraphComponentDescriptor final
|
||||
// Every single `ParagraphShadowNode` will have a reference to
|
||||
// a shared `TextLayoutManager`.
|
||||
textLayoutManager_ = std::make_shared<TextLayoutManager>(contextContainer);
|
||||
// Every single `ParagraphShadowNode` will have a reference to
|
||||
// a shared `EvictingCacheMap`, a simple LRU cache for Paragraph
|
||||
// measurements.
|
||||
measureCache_ = std::make_unique<ParagraphMeasurementCache>();
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -54,10 +48,6 @@ class ParagraphComponentDescriptor final
|
||||
// and communicate text rendering metrics to mounting layer.
|
||||
paragraphShadowNode->setTextLayoutManager(textLayoutManager_);
|
||||
|
||||
// `ParagraphShadowNode` uses this to cache the results of text rendering
|
||||
// measurements.
|
||||
paragraphShadowNode->setMeasureCache(measureCache_.get());
|
||||
|
||||
paragraphShadowNode->dirtyLayout();
|
||||
|
||||
// All `ParagraphShadowNode`s must have leaf Yoga nodes with properly
|
||||
@@ -67,7 +57,6 @@ class ParagraphComponentDescriptor final
|
||||
|
||||
private:
|
||||
SharedTextLayoutManager textLayoutManager_;
|
||||
std::unique_ptr<ParagraphMeasurementCache const> measureCache_;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <react/attributedstring/AttributedString.h>
|
||||
#include <react/attributedstring/ParagraphAttributes.h>
|
||||
#include <react/core/LayoutConstraints.h>
|
||||
#include <react/utils/SimpleThreadSafeCache.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
using ParagraphMeasurementCacheKey =
|
||||
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
|
||||
using ParagraphMeasurementCacheValue = Size;
|
||||
using ParagraphMeasurementCache = SimpleThreadSafeCache<
|
||||
ParagraphMeasurementCacheKey,
|
||||
ParagraphMeasurementCacheValue,
|
||||
256>;
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
@@ -6,8 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "ParagraphShadowNode.h"
|
||||
#include <Glog/logging.h>
|
||||
#include "ParagraphMeasurementCache.h"
|
||||
|
||||
#include "ParagraphState.h"
|
||||
|
||||
namespace facebook {
|
||||
@@ -33,12 +32,6 @@ void ParagraphShadowNode::setTextLayoutManager(
|
||||
textLayoutManager_ = textLayoutManager;
|
||||
}
|
||||
|
||||
void ParagraphShadowNode::setMeasureCache(
|
||||
ParagraphMeasurementCache const *cache) {
|
||||
ensureUnsealed();
|
||||
measureCache_ = cache;
|
||||
}
|
||||
|
||||
void ParagraphShadowNode::updateStateIfNeeded() {
|
||||
ensureUnsealed();
|
||||
|
||||
@@ -68,18 +61,8 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
|
||||
return {0, 0};
|
||||
}
|
||||
|
||||
ParagraphAttributes const paragraphAttributes =
|
||||
getProps()->paragraphAttributes;
|
||||
|
||||
assert(measureCache_);
|
||||
|
||||
return measureCache_->get(
|
||||
ParagraphMeasurementCacheKey{
|
||||
attributedString, paragraphAttributes, layoutConstraints},
|
||||
[&](ParagraphMeasurementCacheKey const &key) {
|
||||
return textLayoutManager_->measure(
|
||||
attributedString, paragraphAttributes, layoutConstraints);
|
||||
});
|
||||
return textLayoutManager_->measure(
|
||||
attributedString, getProps()->paragraphAttributes, layoutConstraints);
|
||||
}
|
||||
|
||||
void ParagraphShadowNode::layout(LayoutContext layoutContext) {
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <folly/Optional.h>
|
||||
#include <react/components/text/ParagraphMeasurementCache.h>
|
||||
#include <react/components/text/ParagraphProps.h>
|
||||
#include <react/components/text/ParagraphState.h>
|
||||
#include <react/components/text/TextShadowNode.h>
|
||||
@@ -57,15 +56,6 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
|
||||
*/
|
||||
void setTextLayoutManager(SharedTextLayoutManager textLayoutManager);
|
||||
|
||||
/*
|
||||
* Associates a shared LRU cache with the node.
|
||||
* `ParagraphShadowNode` uses this to cache the results of
|
||||
* text rendering measurements.
|
||||
* By design, the ParagraphComponentDescriptor outlives all
|
||||
* shadow nodes, so it's safe for this to be a raw pointer.
|
||||
*/
|
||||
void setMeasureCache(ParagraphMeasurementCache const *cache);
|
||||
|
||||
#pragma mark - LayoutableShadowNode
|
||||
|
||||
void layout(LayoutContext layoutContext) override;
|
||||
@@ -79,7 +69,6 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
|
||||
void updateStateIfNeeded();
|
||||
|
||||
SharedTextLayoutManager textLayoutManager_;
|
||||
ParagraphMeasurementCache const *measureCache_;
|
||||
|
||||
/*
|
||||
* Cached attributed string that represents the content of the subtree started
|
||||
|
||||
@@ -26,6 +26,18 @@ Size TextLayoutManager::measure(
|
||||
AttributedString attributedString,
|
||||
ParagraphAttributes paragraphAttributes,
|
||||
LayoutConstraints layoutConstraints) const {
|
||||
return measureCache_.get(
|
||||
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints},
|
||||
[&](MeasureCacheKey const &key) {
|
||||
return doMeasure(
|
||||
attributedString, paragraphAttributes, layoutConstraints);
|
||||
});
|
||||
}
|
||||
|
||||
Size TextLayoutManager::doMeasure(
|
||||
AttributedString attributedString,
|
||||
ParagraphAttributes paragraphAttributes,
|
||||
LayoutConstraints layoutConstraints) const {
|
||||
const jni::global_ref<jobject> &fabricUIManager =
|
||||
contextContainer_->at<jni::global_ref<jobject>>("FabricUIManager");
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <react/attributedstring/ParagraphAttributes.h>
|
||||
#include <react/core/LayoutConstraints.h>
|
||||
#include <react/utils/ContextContainer.h>
|
||||
#include <react/utils/SimpleThreadSafeCache.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
@@ -45,9 +46,18 @@ class TextLayoutManager {
|
||||
void *getNativeTextLayoutManager() const;
|
||||
|
||||
private:
|
||||
void *self_;
|
||||
Size doMeasure(
|
||||
AttributedString attributedString,
|
||||
ParagraphAttributes paragraphAttributes,
|
||||
LayoutConstraints layoutConstraints) const;
|
||||
|
||||
using MeasureCacheKey =
|
||||
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
|
||||
using MeasureCache = SimpleThreadSafeCache<MeasureCacheKey, Size, 256>;
|
||||
|
||||
void *self_;
|
||||
ContextContainer::Shared contextContainer_;
|
||||
MeasureCache measureCache_{};
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <react/attributedstring/ParagraphAttributes.h>
|
||||
#include <react/core/LayoutConstraints.h>
|
||||
#include <react/utils/ContextContainer.h>
|
||||
#include <react/utils/SimpleThreadSafeCache.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
@@ -44,7 +45,12 @@ class TextLayoutManager {
|
||||
void *getNativeTextLayoutManager() const;
|
||||
|
||||
private:
|
||||
using MeasureCacheKey =
|
||||
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
|
||||
using MeasureCache = SimpleThreadSafeCache<MeasureCacheKey, Size, 256>;
|
||||
|
||||
void *self_;
|
||||
MeasureCache measureCache_{};
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
|
||||
@@ -34,10 +34,13 @@ Size TextLayoutManager::measure(
|
||||
ParagraphAttributes paragraphAttributes,
|
||||
LayoutConstraints layoutConstraints) const
|
||||
{
|
||||
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
|
||||
return [textLayoutManager measureWithAttributedString:attributedString
|
||||
paragraphAttributes:paragraphAttributes
|
||||
layoutConstraints:layoutConstraints];
|
||||
return measureCache_.get(
|
||||
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints}, [&](MeasureCacheKey const &key) {
|
||||
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
|
||||
return [textLayoutManager measureWithAttributedString:attributedString
|
||||
paragraphAttributes:paragraphAttributes
|
||||
layoutConstraints:layoutConstraints];
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
|
||||
Reference in New Issue
Block a user