From 4921903663ab1da9ad8a409db1a834b5de6c826c Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Sat, 10 Jun 2023 23:33:21 +0300 Subject: [PATCH] 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 --- src/unordered_lru_cache.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unordered_lru_cache.h b/src/unordered_lru_cache.h index 8a364b8547..7208737dde 100644 --- a/src/unordered_lru_cache.h +++ b/src/unordered_lru_cache.h @@ -36,7 +36,6 @@ public: template void _emplace(const Key& key, Value2&& v) { - truncate_if_needed(); auto it = cacheMap.find(key); if (it == cacheMap.end()) { cacheMap.emplace(key, std::make_pair(std::forward(v), accessCounter++)); @@ -44,6 +43,7 @@ public: it->second.first = std::forward(v); it->second.second = accessCounter++; } + truncate_if_needed(); } void emplace(const Key& key, Value&& v)