code wiki / (root) / lru.nx

lru.nx

buildroot/runtime/lru.nx

4720 B161 linesdepth 4pulls 5 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

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

syscalls.nx map.nx dlist.nx lru.nx

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

main lru_new map_new dlist_new lru_size lru_put map_get map_hash_i64 map_entry_at dlist_move_to_front dlist_remove dlist_push_front dlist_insert_after dlist_pop_back dlist_empty dlist_remove ↻ map_remove map_hash_i64 ↻ map_entry_at ↻ dnode_new dlist_push_front ↻ map_insert map_grow map_hash_i64 ↻ map_entry_at ↻ lru_get map_get ↻ dlist_move_to_front ↻ lru_delete map_get ↻ dlist_remove ↻ map_remove ↻

structs

34struct LRUEntry {
39struct LRU {

consts

none

functions

46func lru_new(cap: i64) -> *LRU {
called by 1: main calls 2: map_newdlist_new
61func lru_get(c: *LRU, key: i64, out: *i64) -> i64 {
called by 1: main calls 2: map_getdlist_move_to_front
73func lru_put(c: *LRU, key: i64, val: i64) -> i64 {
110func lru_delete(c: *LRU, key: i64) -> i64 {
called by 1: main calls 3: map_getdlist_removemap_remove
122func lru_size(c: *LRU) -> i64 {
called by 1: main
127func main() -> i64 {