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:
Родитель
17cb6a8aeb
Коммит
c61398d8f9
|
@ -7,47 +7,21 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <folly/container/EvictingCacheMap.h>
|
||||
|
||||
#include <react/attributedstring/AttributedString.h>
|
||||
#include <react/attributedstring/ParagraphAttributes.h>
|
||||
#include <react/core/LayoutConstraints.h>
|
||||
#include <react/utils/SimpleThreadSafeCache.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
using ParagraphMeasurementCacheKey =
|
||||
std::tuple<AttributedString, ParagraphAttributes, LayoutConstraints>;
|
||||
using ParagraphMeasurementCacheValue = Size;
|
||||
|
||||
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<
|
||||
using ParagraphMeasurementCache = SimpleThreadSafeCache<
|
||||
ParagraphMeasurementCacheKey,
|
||||
ParagraphMeasurementCacheValue>
|
||||
cache_;
|
||||
mutable std::mutex mutex_;
|
||||
};
|
||||
ParagraphMeasurementCacheValue,
|
||||
256>;
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
|
|
|
@ -59,29 +59,22 @@ void ParagraphShadowNode::updateLocalDataIfNeeded() {
|
|||
|
||||
Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
|
||||
AttributedString attributedString = getAttributedString();
|
||||
const ParagraphAttributes attributes = getProps()->paragraphAttributes;
|
||||
|
||||
auto makeMeasurements = [&] {
|
||||
return textLayoutManager_->measure(
|
||||
attributedString, getProps()->paragraphAttributes, layoutConstraints);
|
||||
};
|
||||
const ParagraphAttributes paragraphAttributes =
|
||||
getProps()->paragraphAttributes;
|
||||
|
||||
// Cache results of this function so we don't need to call measure()
|
||||
// repeatedly
|
||||
if (measureCache_ != nullptr) {
|
||||
ParagraphMeasurementCacheKey cacheKey =
|
||||
std::make_tuple(attributedString, attributes, layoutConstraints);
|
||||
if (measureCache_->exists(cacheKey)) {
|
||||
return measureCache_->get(cacheKey);
|
||||
// repeatedly.
|
||||
if (measureCache_) {
|
||||
return measureCache_->get(
|
||||
ParagraphMeasurementCacheKey{attributedString, paragraphAttributes, layoutConstraints},
|
||||
[&](const ParagraphMeasurementCacheKey &key) {
|
||||
return textLayoutManager_->measure(
|
||||
attributedString, paragraphAttributes, layoutConstraints);
|
||||
});
|
||||
}
|
||||
|
||||
auto measuredSize = makeMeasurements();
|
||||
measureCache_->set(cacheKey, measuredSize);
|
||||
|
||||
return measuredSize;
|
||||
}
|
||||
|
||||
return makeMeasurements();
|
||||
return textLayoutManager_->measure(
|
||||
attributedString, paragraphAttributes, layoutConstraints);
|
||||
}
|
||||
|
||||
void ParagraphShadowNode::layout(LayoutContext layoutContext) {
|
||||
|
|
Загрузка…
Ссылка в новой задаче