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:
parent
cd61e922ff
commit
2f2e04142e
1 changed files with 8 additions and 4 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue