MCPcopy Index your code
hub / github.com/billythedummy/const-lru

github.com/billythedummy/const-lru @v1.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.0 ↗ · + Follow
240 symbols 617 edges 30 files 106 documented · 44%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

const-lru

A simple no_std, non-hashing, constant-capacity, constant-memory-usage LRU cache.

The data structure is backed by a couple of const-generic arrays, resulting in all required memory being allocated up-front.

Design

The LRU cache struct is laid out in a struct-of-arrays format: all keys are in 1 array, all values are in another array.

A sorted index over the keys is also stored in the struct to allow for O(log N) lookup times using binary search.

LRU-ordering is implemented using a doubly-linked list, but with array indices instead of pointers. Following the struct-of-arrays format, all the next-link array indices are in one array while all the prev-link array indices are in another array.

To maximize space-efficiency, the last optional generic I specifies the index type, which can be set to an unsigned primitive int type with smaller bitwidth than usize, as long as it's wide enough to store the cache's capacity.

use const_lru::ConstLru;
use core::mem;

assert_eq!(mem::align_of::<ConstLru<u8, u8, 255>>(), 8);
assert_eq!(mem::size_of::<ConstLru<u8, u8, 255>>(), 6656);

assert_eq!(mem::align_of::<ConstLru<u8, u8, 255, u8>>(), 1);
assert_eq!(mem::size_of::<ConstLru<u8, u8, 255, u8>>(), 1278);

Time complexity

where N is number of elements:

  • Retrieval: O(log N) lookup using the sorted index
  • Insertion: O(log N) lookup using the sorted index + O(N) to modify the sorted index (bitwise-copy of index types similar to Vec)
  • Deletion: O(log N) lookup using the sorted index + O(N) to modify the sorted index (bitwise-copy of index types similar to Vec)
  • Length fetching: O(1) since it's stored in the struct
  • Retrieving MRU element: O(1) using .iter().next()
  • Retrieving LRU element: O(1) using .iter().next_back()
  • Retrieving entry of smallest key: O(1) using .iter_key_order().next()
  • Retrieving entry of largest key: O(1) using .iter_key_order().next_back()

Motivation

Most, if not all, general LRU cache implementations (including but not limited to associative-cache, caches, clru, hashlink, lru) rely on one-or-more hashmaps to give O(1) op times. While fast, this makes their usage less well-suited for memory-constrained environments like embedded systems since hashmaps may rehash and reallocate more memory.

ConstLru on the other hand is designed to have a fixed size known at compile-time, but gives up a O(1) hashing-based lookup for a O(log N) binary-search-based lookup and O(N) inserts and removes.

uluru is another fixed-capacity LRU-cache implementation that uses less memory but has O(n) lookup times.

Extension points exported contracts — how you extend this code

Get (Interface)
(no doc) [3 implementers]
benches/common/traits.rs
Insert (Interface)
(no doc) [3 implementers]
benches/common/traits.rs
Remove (Interface)
(no doc) [3 implementers]
benches/common/traits.rs
CreateNew (Interface)
(no doc) [3 implementers]
benches/common/traits.rs

Core symbols most depended-on inside this repo

insert
called by 51
src/lib.rs
clone
called by 30
src/lib.rs
iter
called by 20
src/lib.rs
len
called by 10
src/lib.rs
move_to_head
called by 8
src/lib.rs
cap
called by 8
src/lib.rs
get_index_of
called by 8
src/lib.rs
has_ended
called by 8
src/iters/double_ended_iter_cursors.rs

Shape

Function 120
Method 101
Class 13
Interface 4
Enum 2

Languages

Rust100%

Modules by API surface

src/lib.rs36 symbols
tests/test_iters.rs18 symbols
tests/test_three_cap.rs14 symbols
benches/lru_to_mru/mod.rs14 symbols
src/entry/occupied.rs12 symbols
benches/remove/mod.rs12 symbols
benches/insert/mod.rs12 symbols
benches/get_mru/mod.rs12 symbols
tests/test_mem_safety.rs11 symbols
src/iters/iter_key_order.rs9 symbols
src/iters/double_ended_iter_cursors.rs8 symbols
benches/common/traits.rs8 symbols

For agents

$ claude mcp add const-lru \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact