Fabric: Using SimpleThreadSafeCache in ParagraphMeasurementCache

Summary: `ParagraphMeasurementCache` was replaced with templated version of itself.

Reviewed By: mdvacca

Differential Revision: D14296515

fbshipit-source-id: 29e370f07baf14b25430f85a06f603907aed5563
This commit is contained in:
Valentin Shergin 2019-03-03 12:04:03 -08:00 коммит произвёл Facebook Github Bot
Родитель 17cb6a8aeb
Коммит c61398d8f9
2 изменённых файлов: 18 добавлений и 51 удалений

Просмотреть файл

@ -7,47 +7,21 @@
#pragma once #pragma once
#include <folly/container/EvictingCacheMap.h>
#include <react/attributedstring/AttributedString.h> #include <react/attributedstring/AttributedString.h>
#include <react/attributedstring/ParagraphAttributes.h> #include <react/attributedstring/ParagraphAttributes.h>
#include <react/core/LayoutConstraints.h> #include <react/core/LayoutConstraints.h>
#include <react/utils/SimpleThreadSafeCache.h>
namespace facebook { namespace facebook {
namespace react { namespace react {
using ParagraphMeasurementCacheKey = using ParagraphMeasurementCacheKey =
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>; std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
using ParagraphMeasurementCacheValue = Size; using ParagraphMeasurementCacheValue = Size;
using ParagraphMeasurementCache = SimpleThreadSafeCache<
class ParagraphMeasurementCache {
public:
ParagraphMeasurementCache() : cache_{256} {}
bool exists(const ParagraphMeasurementCacheKey &key) const {
std::lock_guard<std::mutex> lock(mutex_);
return cache_.exists(key);
}
ParagraphMeasurementCacheValue get(
const ParagraphMeasurementCacheKey &key) const {
std::lock_guard<std::mutex> lock(mutex_);
return cache_.get(key);
}
void set(
const ParagraphMeasurementCacheKey &key,
const ParagraphMeasurementCacheValue &value) const {
std::lock_guard<std::mutex> lock(mutex_);
cache_.set(key, value);
}
private:
mutable folly::EvictingCacheMap<
ParagraphMeasurementCacheKey, ParagraphMeasurementCacheKey,
ParagraphMeasurementCacheValue> ParagraphMeasurementCacheValue,
cache_; 256>;
mutable std::mutex mutex_;
};
} // namespace react } // namespace react
} // namespace facebook } // namespace facebook

Просмотреть файл

@ -59,29 +59,22 @@ void ParagraphShadowNode::updateLocalDataIfNeeded() {
Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const { Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
AttributedString attributedString = getAttributedString(); AttributedString attributedString = getAttributedString();
const ParagraphAttributes attributes = getProps()->paragraphAttributes; const ParagraphAttributes paragraphAttributes =
getProps()->paragraphAttributes;
auto makeMeasurements = [&] {
return textLayoutManager_->measure(
attributedString, getProps()->paragraphAttributes, layoutConstraints);
};
// 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.
if (measureCache_ != nullptr) { if (measureCache_) {
ParagraphMeasurementCacheKey cacheKey = return measureCache_->get(
std::make_tuple(attributedString, attributes, layoutConstraints); ParagraphMeasurementCacheKey{attributedString, paragraphAttributes, layoutConstraints},
if (measureCache_->exists(cacheKey)) { [&](const ParagraphMeasurementCacheKey &key) {
return measureCache_->get(cacheKey); return textLayoutManager_->measure(
attributedString, paragraphAttributes, layoutConstraints);
});
} }
auto measuredSize = makeMeasurements(); return textLayoutManager_->measure(
measureCache_->set(cacheKey, measuredSize); attributedString, paragraphAttributes, layoutConstraints);
return measuredSize;
}
return makeMeasurements();
} }
void ParagraphShadowNode::layout(LayoutContext layoutContext) { void ParagraphShadowNode::layout(LayoutContext layoutContext) {