fix: off-by-1 in unordered_lru_cache

we should be truncating after (potentially) adding new element, not before we even tried to do so
This commit is contained in:
UdjinM6 2023-06-10 23:33:21 +03:00
parent 62540743ef
commit 4921903663

View File

@ -36,7 +36,6 @@ public:
template<typename Value2> template<typename Value2>
void _emplace(const Key& key, Value2&& v) void _emplace(const Key& key, Value2&& v)
{ {
truncate_if_needed();
auto it = cacheMap.find(key); auto it = cacheMap.find(key);
if (it == cacheMap.end()) { if (it == cacheMap.end()) {
cacheMap.emplace(key, std::make_pair(std::forward<Value2>(v), accessCounter++)); cacheMap.emplace(key, std::make_pair(std::forward<Value2>(v), accessCounter++));
@ -44,6 +43,7 @@ public:
it->second.first = std::forward<Value2>(v); it->second.first = std::forward<Value2>(v);
it->second.second = accessCounter++; it->second.second = accessCounter++;
} }
truncate_if_needed();
} }
void emplace(const Key& key, Value&& v) void emplace(const Key& key, Value&& v)