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:
Joshua Gross 2019-01-31 19:13:46 -08:00 committed by Facebook Github Bot
parent 36916ee99d
commit 10e4a851ad
4 changed files with 15 additions and 19 deletions

View File

@ -34,7 +34,7 @@ class ParagraphComponentDescriptor final
// Every single `ParagraphShadowNode` will have a reference to
// a shared `EvictingCacheMap`, a simple LRU cache for Paragraph
// measurements.
measureCache_ = std::make_shared<ParagraphMeasurementCache>();
measureCache_ = std::make_unique<ParagraphMeasurementCache>();
}
void adopt(UnsharedShadowNode shadowNode) const override {
@ -50,7 +50,7 @@ class ParagraphComponentDescriptor final
// `ParagraphShadowNode` uses this to cache the results of text rendering
// measurements.
paragraphShadowNode->setMeasureCache(measureCache_);
paragraphShadowNode->setMeasureCache(measureCache_.get());
// All `ParagraphShadowNode`s must have leaf Yoga nodes with properly
// setup measure function.
@ -59,7 +59,7 @@ class ParagraphComponentDescriptor final
private:
SharedTextLayoutManager textLayoutManager_;
SharedParagraphMeasurementCache measureCache_;
std::unique_ptr<const ParagraphMeasurementCache> measureCache_;
};
} // namespace react

View File

@ -19,17 +19,11 @@ using ParagraphMeasurementCacheKey =
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
using ParagraphMeasurementCacheValue = Size;
using ParagraphMeasurementCacheHash = std::hash<ParagraphMeasurementCacheKey>;
class ParagraphMeasurementCache;
using SharedParagraphMeasurementCache =
std::shared_ptr<const ParagraphMeasurementCache>;
class ParagraphMeasurementCache {
public:
ParagraphMeasurementCache() : cache_{256} {}
bool exists(ParagraphMeasurementCacheKey &key) const {
bool exists(const ParagraphMeasurementCacheKey &key) const {
std::lock_guard<std::mutex> lock(mutex_);
return cache_.exists(key);
}
@ -42,7 +36,7 @@ class ParagraphMeasurementCache {
void set(
const ParagraphMeasurementCacheKey &key,
ParagraphMeasurementCacheValue &value) const {
const ParagraphMeasurementCacheValue &value) const {
std::lock_guard<std::mutex> lock(mutex_);
cache_.set(key, value);
}

View File

@ -33,7 +33,7 @@ void ParagraphShadowNode::setTextLayoutManager(
}
void ParagraphShadowNode::setMeasureCache(
SharedParagraphMeasurementCache cache) {
const ParagraphMeasurementCache *cache) {
ensureUnsealed();
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()
// repeatedly
ParagraphMeasurementCacheKey hashValue =
ParagraphMeasurementCacheKey cacheKey =
std::make_tuple(attributedString, attributes, layoutConstraints);
if (measureCache_->exists(hashValue)) {
return measureCache_->get(hashValue);
if (measureCache_->exists(cacheKey)) {
return measureCache_->get(cacheKey);
}
Size measuredSize = textLayoutManager_->measure(
auto measuredSize = textLayoutManager_->measure(
attributedString, getProps()->paragraphAttributes, layoutConstraints);
measureCache_->set(hashValue, measuredSize);
measureCache_->set(cacheKey, measuredSize);
return measuredSize;
}

View File

@ -53,8 +53,10 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
* 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(SharedParagraphMeasurementCache cache);
void setMeasureCache(const ParagraphMeasurementCache *cache);
#pragma mark - LayoutableShadowNode
@ -69,7 +71,7 @@ class ParagraphShadowNode : public ConcreteViewShadowNode<
void updateLocalDataIfNeeded();
SharedTextLayoutManager textLayoutManager_;
SharedParagraphMeasurementCache measureCache_;
const ParagraphMeasurementCache *measureCache_;
/*
* Cached attributed string that represents the content of the subtree started