Skip to content

Commit 76328ca

Browse files
JoshuaGrossfacebook-github-bot
authored andcommitted
Address some paragraph measure cache follow-up nits
Summary: Changing some T& to const T&, removing unnecessary shared_ptr. Reviewed By: shergin Differential Revision: D13845619 fbshipit-source-id: 2678f67f24445e3db105619a07534f5200e313fe
1 parent 6b248c9 commit 76328ca

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ParagraphComponentDescriptor final
3434
// Every single `ParagraphShadowNode` will have a reference to
3535
// a shared `EvictingCacheMap`, a simple LRU cache for Paragraph
3636
// measurements.
37-
measureCache_ = std::make_shared<ParagraphMeasurementCache>();
37+
measureCache_ = std::make_unique<ParagraphMeasurementCache>();
3838
}
3939

4040
void adopt(UnsharedShadowNode shadowNode) const override {
@@ -50,7 +50,7 @@ class ParagraphComponentDescriptor final
5050

5151
// `ParagraphShadowNode` uses this to cache the results of text rendering
5252
// measurements.
53-
paragraphShadowNode->setMeasureCache(measureCache_);
53+
paragraphShadowNode->setMeasureCache(measureCache_.get());
5454

5555
// All `ParagraphShadowNode`s must have leaf Yoga nodes with properly
5656
// setup measure function.
@@ -59,7 +59,7 @@ class ParagraphComponentDescriptor final
5959

6060
private:
6161
SharedTextLayoutManager textLayoutManager_;
62-
SharedParagraphMeasurementCache measureCache_;
62+
std::unique_ptr<const ParagraphMeasurementCache> measureCache_;
6363
};
6464

6565
} // namespace react

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,11 @@ using ParagraphMeasurementCacheKey =
1919
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
2020
using ParagraphMeasurementCacheValue = Size;
2121

22-
using ParagraphMeasurementCacheHash = std::hash<ParagraphMeasurementCacheKey>;
23-
24-
class ParagraphMeasurementCache;
25-
using SharedParagraphMeasurementCache =
26-
std::shared_ptr<const ParagraphMeasurementCache>;
27-
2822
class ParagraphMeasurementCache {
2923
public:
3024
ParagraphMeasurementCache() : cache_{256} {}
3125

32-
bool exists(ParagraphMeasurementCacheKey &key) const {
26+
bool exists(const ParagraphMeasurementCacheKey &key) const {
3327
std::lock_guard<std::mutex> lock(mutex_);
3428
return cache_.exists(key);
3529
}
@@ -42,7 +36,7 @@ class ParagraphMeasurementCache {
4236

4337
void set(
4438
const ParagraphMeasurementCacheKey &key,
45-
ParagraphMeasurementCacheValue &value) const {
39+
const ParagraphMeasurementCacheValue &value) const {
4640
std::lock_guard<std::mutex> lock(mutex_);
4741
cache_.set(key, value);
4842
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void ParagraphShadowNode::setTextLayoutManager(
3333
}
3434

3535
void ParagraphShadowNode::setMeasureCache(
36-
SharedParagraphMeasurementCache cache) {
36+
const ParagraphMeasurementCache *cache) {
3737
ensureUnsealed();
3838
measureCache_ = cache;
3939
}
@@ -63,16 +63,16 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
6363

6464
// Cache results of this function so we don't need to call measure()
6565
// repeatedly
66-
ParagraphMeasurementCacheKey hashValue =
66+
ParagraphMeasurementCacheKey cacheKey =
6767
std::make_tuple(attributedString, attributes, layoutConstraints);
68-
if (measureCache_->exists(hashValue)) {
69-
return measureCache_->get(hashValue);
68+
if (measureCache_->exists(cacheKey)) {
69+
return measureCache_->get(cacheKey);
7070
}
7171

72-
Size measuredSize = textLayoutManager_->measure(
72+
auto measuredSize = textLayoutManager_->measure(
7373
attributedString, getProps()->paragraphAttributes, layoutConstraints);
7474

75-
measureCache_->set(hashValue, measuredSize);
75+
measureCache_->set(cacheKey, measuredSize);
7676

7777
return measuredSize;
7878
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
5353
* Associates a shared LRU cache with the node.
5454
* `ParagraphShadowNode` uses this to cache the results of
5555
* text rendering measurements.
56+
* By design, the ParagraphComponentDescriptor outlives all
57+
* shadow nodes, so it's safe for this to be a raw pointer.
5658
*/
57-
void setMeasureCache(SharedParagraphMeasurementCache cache);
59+
void setMeasureCache(const ParagraphMeasurementCache *cache);
5860

5961
#pragma mark - LayoutableShadowNode
6062

@@ -69,7 +71,7 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
6971
void updateLocalDataIfNeeded();
7072

7173
SharedTextLayoutManager textLayoutManager_;
72-
SharedParagraphMeasurementCache measureCache_;
74+
const ParagraphMeasurementCache *measureCache_;
7375

7476
/*
7577
* Cached attributed string that represents the content of the subtree started

0 commit comments

Comments
 (0)