#lru-cache #lru #lazy-evaluation #linked-list #eviction

no-std lazy-lru

A least-recently-used (LRU) cache implementation with lazy eviction

4 releases

0.1.3 Aug 8, 2024
0.1.2 Apr 7, 2024
0.1.1 Apr 7, 2024
0.1.0 Mar 13, 2024

#82 in Caching

Download history 13215/week @ 2024-08-17 15805/week @ 2024-08-24 11616/week @ 2024-08-31 12425/week @ 2024-09-07 5858/week @ 2024-09-14 10770/week @ 2024-09-21 10680/week @ 2024-09-28 12547/week @ 2024-10-05 16875/week @ 2024-10-12 16373/week @ 2024-10-19 17970/week @ 2024-10-26 18596/week @ 2024-11-02 17605/week @ 2024-11-09 14951/week @ 2024-11-16 13304/week @ 2024-11-23 22309/week @ 2024-11-30

71,672 downloads per month
Used in 18 crates (2 directly)

MIT license

23KB
425 lines

Lazy LRU cache

Typically, an LRU cache is implemented using a combination of a hash map and a doubly linked list. The doubly linked list keeps track of the order in which the items were accessed. When an entry is accessed or inserted into the cache, its respective reference is moved to (or inserted at) the front of the linked list. Doing so, the least recently used item is always at the back of the linked list and is evicted as soon as the cache size exceeds its designated capacity.

This crate instead implements an alternative variant of LRU cache with lazy eviction:

  • Each entry maintains an associated ordinal value representing when the entry was last accessed.
  • The cache is allowed to grow up to 2 times the specified capacity with no evictions, at which point, the excess entries are evicted based on LRU policy in linear time resulting in an amortized O(1) performance.

In many use cases which can allow the cache to store 2 times the capacity and can tolerate the amortized nature of performance, this results in a better average performance as shown by the benchmarks in this crate:

test bench_get_eager ... bench:      21,434 ns/iter (+/- 3,565)
test bench_get_lazy  ... bench:      16,514 ns/iter (+/- 385)
test bench_put_eager ... bench:      52,277 ns/iter (+/- 25,473)
test bench_put_lazy  ... bench:      33,117 ns/iter (+/- 5,057)

Additionally, with the eager implementation, lookups require a mutable reference &mut self to allow updating internal linked list. In a multi-threaded setting, this requires an exclusive write-lock on the cache even on the read path, which can exacerbate lock contentions. With lazy eviction, the ordinal values can be updated using atomic operations, allowing shared lock for lookups.

Dependencies

~2MB
~27K SLoC