On this page

0067. Cache native builds by complete compilation content

Context

Repeated native builds invoke an external optimizing C compiler even when its inputs are unchanged. Harness has no modules yet and must use std only.

Decision

Cache successful C-path build and run executables under HARNESS_CACHE_DIR, else $XDG_CACHE_HOME/harness, else $HOME/.cache/harness, in a v1 directory. HARNESS_NO_CACHE=1 bypasses all cache reads/writes. Cache errors are misses.

The full length-framed key contains emitted C, actual compiler program/argv (CC and all driver flags), exact CC --version stdout/stderr, Harness version, embedded runtime headers, cwd and sorted environment (except cache controls). Normalize only transient driver input/output paths; output filenames can differ. Use stable FNV-1a 64-bit directory names and compare the entire stored key on every hit, so hash collisions cannot return another compilation's executable.

Bundle key, executable, warnings and length/FNV checksums in one artifact in a unique sibling directory, then atomically rename the directory into place. Never update a published artifact in place. Read one artifact handle to avoid mixing keys/payloads across colliding replacements. Corrupt/incomplete entries rebuild. Replay warnings on hits; restore via sibling-file rename with current umask, preserving unrelated hardlink/symlink targets. New cache directories are private (0700 on Unix), since keys include environment.

Cap completed entries at 256 MiB, counting keys, binaries, warnings and manifests; skip larger entries. After publication evict oldest directory mtimes first (FIFO, not access-time LRU). Concurrency may temporarily exceed the cap; the next successful publication retries eviction. Staging directories are excluded and normally removed by RAII; a killed writer's .tmp-* directories can be deleted manually. Incomplete staging directories are never lookup candidates.

Alternatives considered

Consequences

Warm builds still check/lower/emit and probe CC, but skip C compilation. Cache hits copy a validated executable to the requested destination. FNV is an accidental-corruption check, not authentication: this is a local trusted cache. External files supplied through custom CC flags/wrappers are not dependency tracked; use HARNESS_NO_CACHE=1 when those inputs change independently. No language or diagnostic changes; no new dependency. M9 owns per-module builds.