On this page

0236. Native cache capacity and LRU eviction

Context

ADR-0067 capped the v1 executable cache at 256 MiB with oldest-first (FIFO) eviction, and ADR-0210's ffi-v1 namespace reused that cap. One golden suite run now needs about 323 MB. With a fresh HARNESS_CACHE_DIR, the second run of cargo test --release --test golden still made 833 of 838 C-driver calls: the run visits programs in the same order, so FIFO evicted each entry just before the next run needed it. Every insert also listed and sized the whole cache directory. Golden, tui and heap_programs run twice each into one fresh cache fill 505 MB (bench/native-cache/RESULTS.md).

Decision

Give each namespace an independent default capacity of 2 GiB, about four times that two-run footprint. The headroom covers extra key sets from a different CC, flags or environment (about 323 MB each for golden) and suite growth. HARNESS_CACHE_MAX_MB overrides the cap with a positive decimal integer in MiB. Missing, invalid, zero, non-Unicode or overflowing values use the default, the same way other cache variables ignore values they do not recognize. The variable is excluded from cache keys. The effective cap bounds entry admission, artifact reads and the FFI entry guard.

After a successful restore, including FFI restores, best-effort set the entry directory's mtime to now. Errors or races there never turn a hit into a miss. Eviction sorts entries by oldest directory mtime first, an approximate LRU under concurrent readers.

A hidden .budget file in each namespace, under an advisory file lock, records the cap and a conservative byte count, so most inserts skip the directory walk. A writer scans only when the ledger is missing or corrupt, the cap changed, or the count would exceed the cap. It then deletes the oldest entries until usage is at most 90% of the cap and records the remaining total. Concurrent publications and replacements can only overcount, which causes an earlier scan. A crash between publication and accounting can briefly undercount until the next scan. If the lock is unavailable, the writer scans and evicts as before. Publication and restoration stay atomic and immutable; no dependencies are added.

Alternatives considered

Consequences

Golden run 2 drops from 833 C-driver calls (70.8 s) to 15 (10.0 s), including probes. Cold run 1 is unchanged within noise (79.4 s before, 72.7 s after, load avg 20–30). With HARNESS_CACHE_MAX_MB=200, eviction kept a full golden run under the cap, and the ledger matched the bytes on disk. The cap stays best effort across crashes and concurrent publishers. Default disk use can grow to 2 GiB per namespace. ADR-0236 supersedes only the capacity and eviction provisions of ADR-0067 and ADR-0210; their cache keys and artifact formats (FMT_VERSION 1) stay in force.