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
|
#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 {
|
ParagraphMeasurementCacheKey,
|
||||||
public:
|
ParagraphMeasurementCacheValue,
|
||||||
ParagraphMeasurementCache() : cache_{256} {}
|
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,
|
|
||||||
ParagraphMeasurementCacheValue>
|
|
||||||
cache_;
|
|
||||||
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();
|
|
||||||
measureCache_->set(cacheKey, measuredSize);
|
|
||||||
|
|
||||||
return measuredSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return makeMeasurements();
|
return textLayoutManager_->measure(
|
||||||
|
attributedString, paragraphAttributes, layoutConstraints);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParagraphShadowNode::layout(LayoutContext layoutContext) {
|
void ParagraphShadowNode::layout(LayoutContext layoutContext) {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче