1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-24 22:11:15 +02:00

libutil: Simplify LRUCache::get by using list splice

Splicing the list element to the back can be done in
a much simpler and concise way without the need for
erasing and re-inserting the element. Doing it this
way is equivalent to just moving node pointers around,
whereas inserting/erasing allocates/deallocates new nodes.
This commit is contained in:
Sergei Zimmerman 2025-05-14 22:05:53 +00:00
parent cd61e922ff
commit 2f2e04142e
No known key found for this signature in database
GPG key ID: A9B0B557CA632325

View file

@ -93,12 +93,16 @@ public:
/**
* Move this item to the back of the LRU list.
*
* Think of std::list iterators as stable pointers to the list node,
* which never get invalidated. Thus, we can reuse the same lru list
* element and just splice it to the back of the list without the need
* to update its value in the key -> list iterator map.
*/
lru.erase(i->second.first.it);
auto j = lru.insert(lru.end(), i);
i->second.first.it = j;
auto & [it, value] = i->second;
lru.splice(/*pos=*/lru.end(), /*other=*/lru, it.it);
return i->second.second;
return value;
}
size_t size() const