26 std::shared_ptr<const GenericKey>
key;
28 std::shared_ptr<CachedValue>
value;
70 ->store(new_time, std::memory_order_relaxed);
71 static_assert(
sizeof(
int64_t) ==
sizeof(std::atomic<int64_t>));
75 const FunctionRef<std::unique_ptr<CachedValue>()> compute_fn)
84 if (cache.
map.
lookup(accessor, std::ref(key))) {
86 return accessor->second.value;
93 std::shared_ptr<CachedValue>
result = compute_fn();
99 const bool newly_inserted = cache.
map.
add(accessor, std::ref(key));
100 if (!newly_inserted) {
103 return accessor->second.value;
109 const_cast<std::reference_wrapper<const GenericKey> &
>(accessor->first) = std::ref(
110 *accessor->second.key);
113 accessor->second.value =
result;
122 cache.
keys.append(&accessor->first.get());
159 const bool ok_to_remove = predicate(key);
160 predicate_results[
i] = ok_to_remove;
172 const bool success = cache.
map.
remove(key);
179 return predicate_results[index];
189 if (old_size < approximate_limit) {
203 keys_with_time.
append({accessor->second.last_use_time, key});
206 std::sort(keys_with_time.
begin(), keys_with_time.
end());
207 std::reverse(keys_with_time.
begin(), keys_with_time.
end());
212 std::optional<int> first_bad_index;
232 if (!first_bad_index) {
239 bool need_memory_recount =
false;
241 *first_bad_index += 1;
242 if (*first_bad_index == keys_with_time.
size()) {
247 need_memory_recount =
true;
259 cache.
keys.append(keys_with_time[
i].second);
262 if (need_memory_recount) {
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
bool lookup(Accessor &accessor, const Key &key)
bool remove(const Key &key)
bool add(Accessor &accessor, const Key &key)
virtual std::unique_ptr< GenericKey > to_storable() const =0
constexpr IndexRange take_front(int64_t n) const
constexpr IndexRange drop_front(int64_t n) const
void append(const T &value)
IndexRange index_range() const
static void try_enforce_limit()
void set_approximate_size_limit(int64_t limit_in_bytes)
std::shared_ptr< CachedValue > get_base(const GenericKey &key, FunctionRef< std::unique_ptr< CachedValue >()> compute_fn)
void remove_if(FunctionRef< bool(const GenericKey &)> predicate)
static void set_new_logical_time(const StoredValue &stored_value, const int64_t new_time)
static Cache & get_cache()
ConcurrentMap< std::reference_wrapper< const GenericKey >, StoredValue > CacheMap
Vector< const GenericKey * > keys
std::atomic< int64_t > approximate_limit
std::atomic< int64_t > size_in_bytes
std::atomic< int64_t > logical_time
std::shared_ptr< CachedValue > value
std::shared_ptr< const GenericKey > key