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;
121 accessor->second.value->count_memory(memory_counter);
122 cache.
keys.append(&accessor->first.get());
160 if (old_size < approximate_limit) {
174 keys_with_time.
append({accessor->second.last_use_time, key});
177 std::sort(keys_with_time.
begin(), keys_with_time.
end());
178 std::reverse(keys_with_time.
begin(), keys_with_time.
end());
183 std::optional<int> first_bad_index;
187 const GenericKey &key = *keys_with_time[i].second;
192 accessor->second.value->count_memory(memory_counter);
203 if (!first_bad_index) {
210 bool need_memory_recount =
false;
212 *first_bad_index += 1;
213 if (*first_bad_index == keys_with_time.
size()) {
218 need_memory_recount =
true;
223 const GenericKey &key = *keys_with_time[i].second;
230 cache.
keys.append(keys_with_time[i].second);
233 if (need_memory_recount) {
239 const GenericKey &key = *keys_with_time[i].second;
244 accessor->second.value->count_memory(memory_counter);