Skip to content

Commit f32aa2a

Browse files
sherginfacebook-github-bot
authored andcommitted
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
1 parent 5f0435f commit f32aa2a

File tree

8 files changed

+39
-74
lines changed

8 files changed

+39
-74
lines changed

ReactCommon/fabric/components/text/paragraph/ParagraphComponentDescriptor.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
#pragma once
99

10-
#include "ParagraphMeasurementCache.h"
1110
#include "ParagraphShadowNode.h"
1211

13-
#include <folly/container/EvictingCacheMap.h>
1412
#include <react/config/ReactNativeConfig.h>
1513
#include <react/core/ConcreteComponentDescriptor.h>
1614
#include <react/textlayoutmanager/TextLayoutManager.h>
@@ -36,10 +34,6 @@ class ParagraphComponentDescriptor final
3634
// Every single `ParagraphShadowNode` will have a reference to
3735
// a shared `TextLayoutManager`.
3836
textLayoutManager_ = std::make_shared<TextLayoutManager>(contextContainer);
39-
// Every single `ParagraphShadowNode` will have a reference to
40-
// a shared `EvictingCacheMap`, a simple LRU cache for Paragraph
41-
// measurements.
42-
measureCache_ = std::make_unique<ParagraphMeasurementCache>();
4337
}
4438

4539
protected:
@@ -54,10 +48,6 @@ class ParagraphComponentDescriptor final
5448
// and communicate text rendering metrics to mounting layer.
5549
paragraphShadowNode->setTextLayoutManager(textLayoutManager_);
5650

57-
// `ParagraphShadowNode` uses this to cache the results of text rendering
58-
// measurements.
59-
paragraphShadowNode->setMeasureCache(measureCache_.get());
60-
6151
paragraphShadowNode->dirtyLayout();
6252

6353
// All `ParagraphShadowNode`s must have leaf Yoga nodes with properly
@@ -67,7 +57,6 @@ class ParagraphComponentDescriptor final
6757

6858
private:
6959
SharedTextLayoutManager textLayoutManager_;
70-
std::unique_ptr<ParagraphMeasurementCache const> measureCache_;
7160
};
7261

7362
} // namespace react

ReactCommon/fabric/components/text/paragraph/ParagraphMeasurementCache.h

Lines changed: 0 additions & 27 deletions
This file was deleted.

ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
*/
77

88
#include "ParagraphShadowNode.h"
9-
#include <Glog/logging.h>
10-
#include "ParagraphMeasurementCache.h"
9+
1110
#include "ParagraphState.h"
1211

1312
namespace facebook {
@@ -33,12 +32,6 @@ void ParagraphShadowNode::setTextLayoutManager(
3332
textLayoutManager_ = textLayoutManager;
3433
}
3534

36-
void ParagraphShadowNode::setMeasureCache(
37-
ParagraphMeasurementCache const *cache) {
38-
ensureUnsealed();
39-
measureCache_ = cache;
40-
}
41-
4235
void ParagraphShadowNode::updateStateIfNeeded() {
4336
ensureUnsealed();
4437

@@ -68,18 +61,8 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
6861
return {0, 0};
6962
}
7063

71-
ParagraphAttributes const paragraphAttributes =
72-
getProps()->paragraphAttributes;
73-
74-
assert(measureCache_);
75-
76-
return measureCache_->get(
77-
ParagraphMeasurementCacheKey{
78-
attributedString, paragraphAttributes, layoutConstraints},
79-
[&](ParagraphMeasurementCacheKey const &key) {
80-
return textLayoutManager_->measure(
81-
attributedString, paragraphAttributes, layoutConstraints);
82-
});
64+
return textLayoutManager_->measure(
65+
attributedString, getProps()->paragraphAttributes, layoutConstraints);
8366
}
8467

8568
void ParagraphShadowNode::layout(LayoutContext layoutContext) {

ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#pragma once
99

1010
#include <folly/Optional.h>
11-
#include <react/components/text/ParagraphMeasurementCache.h>
1211
#include <react/components/text/ParagraphProps.h>
1312
#include <react/components/text/ParagraphState.h>
1413
#include <react/components/text/TextShadowNode.h>
@@ -57,15 +56,6 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
5756
*/
5857
void setTextLayoutManager(SharedTextLayoutManager textLayoutManager);
5958

60-
/*
61-
* Associates a shared LRU cache with the node.
62-
* `ParagraphShadowNode` uses this to cache the results of
63-
* text rendering measurements.
64-
* By design, the ParagraphComponentDescriptor outlives all
65-
* shadow nodes, so it's safe for this to be a raw pointer.
66-
*/
67-
void setMeasureCache(ParagraphMeasurementCache const *cache);
68-
6959
#pragma mark - LayoutableShadowNode
7060

7161
void layout(LayoutContext layoutContext) override;
@@ -79,7 +69,6 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
7969
void updateStateIfNeeded();
8070

8171
SharedTextLayoutManager textLayoutManager_;
82-
ParagraphMeasurementCache const *measureCache_;
8372

8473
/*
8574
* Cached attributed string that represents the content of the subtree started

ReactCommon/fabric/textlayoutmanager/platform/android/TextLayoutManager.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ Size TextLayoutManager::measure(
2626
AttributedString attributedString,
2727
ParagraphAttributes paragraphAttributes,
2828
LayoutConstraints layoutConstraints) const {
29+
return measureCache_.get(
30+
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints},
31+
[&](MeasureCacheKey const &key) {
32+
return doMeasure(
33+
attributedString, paragraphAttributes, layoutConstraints);
34+
});
35+
}
36+
37+
Size TextLayoutManager::doMeasure(
38+
AttributedString attributedString,
39+
ParagraphAttributes paragraphAttributes,
40+
LayoutConstraints layoutConstraints) const {
2941
const jni::global_ref<jobject> &fabricUIManager =
3042
contextContainer_->at<jni::global_ref<jobject>>("FabricUIManager");
3143

ReactCommon/fabric/textlayoutmanager/platform/android/TextLayoutManager.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <react/attributedstring/ParagraphAttributes.h>
1414
#include <react/core/LayoutConstraints.h>
1515
#include <react/utils/ContextContainer.h>
16+
#include <react/utils/SimpleThreadSafeCache.h>
1617

1718
namespace facebook {
1819
namespace react {
@@ -45,9 +46,18 @@ class TextLayoutManager {
4546
void *getNativeTextLayoutManager() const;
4647

4748
private:
48-
void *self_;
49+
Size doMeasure(
50+
AttributedString attributedString,
51+
ParagraphAttributes paragraphAttributes,
52+
LayoutConstraints layoutConstraints) const;
53+
54+
using MeasureCacheKey =
55+
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
56+
using MeasureCache = SimpleThreadSafeCache<MeasureCacheKey, Size, 256>;
4957

58+
void *self_;
5059
ContextContainer::Shared contextContainer_;
60+
MeasureCache measureCache_{};
5161
};
5262

5363
} // namespace react

ReactCommon/fabric/textlayoutmanager/platform/ios/TextLayoutManager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <react/attributedstring/ParagraphAttributes.h>
1414
#include <react/core/LayoutConstraints.h>
1515
#include <react/utils/ContextContainer.h>
16+
#include <react/utils/SimpleThreadSafeCache.h>
1617

1718
namespace facebook {
1819
namespace react {
@@ -44,7 +45,12 @@ class TextLayoutManager {
4445
void *getNativeTextLayoutManager() const;
4546

4647
private:
48+
using MeasureCacheKey =
49+
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
50+
using MeasureCache = SimpleThreadSafeCache<MeasureCacheKey, Size, 256>;
51+
4752
void *self_;
53+
MeasureCache measureCache_{};
4854
};
4955

5056
} // namespace react

ReactCommon/fabric/textlayoutmanager/platform/ios/TextLayoutManager.mm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@
3434
ParagraphAttributes paragraphAttributes,
3535
LayoutConstraints layoutConstraints) const
3636
{
37-
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
38-
return [textLayoutManager measureWithAttributedString:attributedString
39-
paragraphAttributes:paragraphAttributes
40-
layoutConstraints:layoutConstraints];
37+
return measureCache_.get(
38+
MeasureCacheKey{attributedString, paragraphAttributes, layoutConstraints}, [&](MeasureCacheKey const &key) {
39+
RCTTextLayoutManager *textLayoutManager = (__bridge RCTTextLayoutManager *)self_;
40+
return [textLayoutManager measureWithAttributedString:attributedString
41+
paragraphAttributes:paragraphAttributes
42+
layoutConstraints:layoutConstraints];
43+
});
4144
}
4245

4346
} // namespace react

0 commit comments

Comments
 (0)