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 // 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

View File

@ -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);
} }

View File

@ -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;
} }

View File

@ -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