mirror of
https://github.com/status-im/react-native.git
synced 2025-02-23 22:58:19 +00:00
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
This commit is contained in:
parent
36916ee99d
commit
10e4a851ad
@ -34,7 +34,7 @@ class ParagraphComponentDescriptor final
|
|||||||
// Every single `ParagraphShadowNode` will have a reference to
|
// Every single `ParagraphShadowNode` will have a reference to
|
||||||
// a shared `EvictingCacheMap`, a simple LRU cache for Paragraph
|
// a shared `EvictingCacheMap`, a simple LRU cache for Paragraph
|
||||||
// measurements.
|
// measurements.
|
||||||
measureCache_ = std::make_shared<ParagraphMeasurementCache>();
|
measureCache_ = std::make_unique<ParagraphMeasurementCache>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void adopt(UnsharedShadowNode shadowNode) const override {
|
void adopt(UnsharedShadowNode shadowNode) const override {
|
||||||
@ -50,7 +50,7 @@ class ParagraphComponentDescriptor final
|
|||||||
|
|
||||||
// `ParagraphShadowNode` uses this to cache the results of text rendering
|
// `ParagraphShadowNode` uses this to cache the results of text rendering
|
||||||
// measurements.
|
// measurements.
|
||||||
paragraphShadowNode->setMeasureCache(measureCache_);
|
paragraphShadowNode->setMeasureCache(measureCache_.get());
|
||||||
|
|
||||||
// All `ParagraphShadowNode`s must have leaf Yoga nodes with properly
|
// All `ParagraphShadowNode`s must have leaf Yoga nodes with properly
|
||||||
// setup measure function.
|
// setup measure function.
|
||||||
@ -59,7 +59,7 @@ class ParagraphComponentDescriptor final
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
SharedTextLayoutManager textLayoutManager_;
|
SharedTextLayoutManager textLayoutManager_;
|
||||||
SharedParagraphMeasurementCache measureCache_;
|
std::unique_ptr<const ParagraphMeasurementCache> measureCache_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace react
|
} // namespace react
|
||||||
|
@ -19,17 +19,11 @@ using ParagraphMeasurementCacheKey =
|
|||||||
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
|
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
|
||||||
using ParagraphMeasurementCacheValue = Size;
|
using ParagraphMeasurementCacheValue = Size;
|
||||||
|
|
||||||
using ParagraphMeasurementCacheHash = std::hash<ParagraphMeasurementCacheKey>;
|
|
||||||
|
|
||||||
class ParagraphMeasurementCache;
|
|
||||||
using SharedParagraphMeasurementCache =
|
|
||||||
std::shared_ptr<const ParagraphMeasurementCache>;
|
|
||||||
|
|
||||||
class ParagraphMeasurementCache {
|
class ParagraphMeasurementCache {
|
||||||
public:
|
public:
|
||||||
ParagraphMeasurementCache() : cache_{256} {}
|
ParagraphMeasurementCache() : cache_{256} {}
|
||||||
|
|
||||||
bool exists(ParagraphMeasurementCacheKey &key) const {
|
bool exists(const ParagraphMeasurementCacheKey &key) const {
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
return cache_.exists(key);
|
return cache_.exists(key);
|
||||||
}
|
}
|
||||||
@ -42,7 +36,7 @@ class ParagraphMeasurementCache {
|
|||||||
|
|
||||||
void set(
|
void set(
|
||||||
const ParagraphMeasurementCacheKey &key,
|
const ParagraphMeasurementCacheKey &key,
|
||||||
ParagraphMeasurementCacheValue &value) const {
|
const ParagraphMeasurementCacheValue &value) const {
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
cache_.set(key, value);
|
cache_.set(key, value);
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ void ParagraphShadowNode::setTextLayoutManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ParagraphShadowNode::setMeasureCache(
|
void ParagraphShadowNode::setMeasureCache(
|
||||||
SharedParagraphMeasurementCache cache) {
|
const ParagraphMeasurementCache *cache) {
|
||||||
ensureUnsealed();
|
ensureUnsealed();
|
||||||
measureCache_ = cache;
|
measureCache_ = cache;
|
||||||
}
|
}
|
||||||
@ -63,16 +63,16 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
|
|||||||
|
|
||||||
// Cache results of this function so we don't need to call measure()
|
// Cache results of this function so we don't need to call measure()
|
||||||
// repeatedly
|
// repeatedly
|
||||||
ParagraphMeasurementCacheKey hashValue =
|
ParagraphMeasurementCacheKey cacheKey =
|
||||||
std::make_tuple(attributedString, attributes, layoutConstraints);
|
std::make_tuple(attributedString, attributes, layoutConstraints);
|
||||||
if (measureCache_->exists(hashValue)) {
|
if (measureCache_->exists(cacheKey)) {
|
||||||
return measureCache_->get(hashValue);
|
return measureCache_->get(cacheKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
Size measuredSize = textLayoutManager_->measure(
|
auto measuredSize = textLayoutManager_->measure(
|
||||||
attributedString, getProps()->paragraphAttributes, layoutConstraints);
|
attributedString, getProps()->paragraphAttributes, layoutConstraints);
|
||||||
|
|
||||||
measureCache_->set(hashValue, measuredSize);
|
measureCache_->set(cacheKey, measuredSize);
|
||||||
|
|
||||||
return measuredSize;
|
return measuredSize;
|
||||||
}
|
}
|
||||||
|
@ -53,8 +53,10 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
|
|||||||
* Associates a shared LRU cache with the node.
|
* Associates a shared LRU cache with the node.
|
||||||
* `ParagraphShadowNode` uses this to cache the results of
|
* `ParagraphShadowNode` uses this to cache the results of
|
||||||
* text rendering measurements.
|
* text rendering measurements.
|
||||||
|
* By design, the ParagraphComponentDescriptor outlives all
|
||||||
|
* shadow nodes, so it's safe for this to be a raw pointer.
|
||||||
*/
|
*/
|
||||||
void setMeasureCache(SharedParagraphMeasurementCache cache);
|
void setMeasureCache(const ParagraphMeasurementCache *cache);
|
||||||
|
|
||||||
#pragma mark - LayoutableShadowNode
|
#pragma mark - LayoutableShadowNode
|
||||||
|
|
||||||
@ -69,7 +71,7 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
|
|||||||
void updateLocalDataIfNeeded();
|
void updateLocalDataIfNeeded();
|
||||||
|
|
||||||
SharedTextLayoutManager textLayoutManager_;
|
SharedTextLayoutManager textLayoutManager_;
|
||||||
SharedParagraphMeasurementCache measureCache_;
|
const ParagraphMeasurementCache *measureCache_;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Cached attributed string that represents the content of the subtree started
|
* Cached attributed string that represents the content of the subtree started
|
||||||
|
Loading…
x
Reference in New Issue
Block a user