lru.nx
buildroot/runtime/lru.nx
about
lru.nx -- Least-Recently-Used cache.
Composes map.nx (key -> node) + dlist.nx (recency order).
Classic design, same shape as the LRU in every modern kernel
page cache + every language std lib's LinkedHashMap:
- get(k) -> value + moves node to front
- put(k, v) -> insert or update + moves node to front,
evicts tail if over capacity
This is the first module composing two runtime data structures
end-to-end. Proves the shelves interlock cleanly.
Storage per entry: DNode with data pointer holding a
LRUEntry struct {key, val}. Map stores key -> node pointer
so get() is O(1) amortised.
Use cases: compiled-template cache (nishi-pages), route
table in HTTP router, DNS resolver cache, prepared-SQL cache,
image thumbnail cache.
Invariants:
L1 size <= capacity at all times (enforced by eviction
on insert).
L2 Map and dlist stay in sync: every map entry value is a
live DNode in the dlist; every live dlist node has an
entry in the map.
L3 get(missing) returns -1 without mutating state.
L4 Capacity of 0 is legal (no entries ever admitted).
dependencies 3 imports · 0 importers
imports: syscalls.nxmap.nxdlist.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| 34 | struct LRUEntry { |
| 39 | struct LRU { |
consts
| none |
functions
| 46 | func lru_new(cap: i64) -> *LRU { |
| 61 | func lru_get(c: *LRU, key: i64, out: *i64) -> i64 { |
| 73 | func lru_put(c: *LRU, key: i64, val: i64) -> i64 {
called by 1: main calls 7: map_getdlist_move_to_frontdlist_pop_backmap_removednode_newdlist_push_front+1 |
| 110 | func lru_delete(c: *LRU, key: i64) -> i64 { |
| 122 | func lru_size(c: *LRU) -> i64 {
called by 1: main |
| 127 | func main() -> i64 { |