Files
react-native/ReactCommon/fabric/components/text/paragraph/ParagraphMeasurementCache.h
Joshua Gross a9049442f7 Fabric: Use LRU to cache results of ParagraphShadowNode::measure
Summary: Use a folly LRU implementation to cache results of ParagraphShadowNode::measure, which Yoga asks for repeatedly. Should have a substantial speed improvement on Android and iOS, or at least that's the dream.

Reviewed By: mdvacca

Differential Revision: D13795808

fbshipit-source-id: 5716af0fe0517a72716e48113c8125bb788735d7
2019-01-25 17:00:51 -08:00

60 lines
1.6 KiB
C++

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <folly/container/EvictingCacheMap.h>
#include <react/attributedstring/AttributedString.h>
#include <react/attributedstring/ParagraphAttributes.h>
#include <react/core/LayoutConstraints.h>
namespace facebook {
namespace react {
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 {
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,
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 facebook