Files
metal2vulkan/docs
Anees Iqbal 8bc2e7e1ac Keep the content directory's large records as content-defined chunks, each shared run once
NVIDIA's pipeline cache data (620.70.06, an RTX 3090) held one 1,851,552-byte section, the same
bytes, in 3,565 of um790's records: its 11,853 NVIDIA records were 8,061 MB. A read-only measure
of that node's 42,860 records (cut as below, averages of 16, 32 and 64 KiB) stores them in 296 MB
at 32 KiB (NVIDIA's in 180 MB, 2.2%); 64 KiB keeps each shared run's end chunks apart (634 MB),
16 KiB doubles the files a read opens for 1.3x the bytes.

shader_cache/chunks.rs (new), store.rs, driver.rs:
- A content record of 256 KiB or more is cut by a gear rolling hash (FastCDC's normalized cut,
  8 KiB to 128 KiB around 32 KiB): boundaries from the bytes alone, nothing of any driver's
  format read. Each chunk is kept once at <root>/chunks/<2 hex>/<SHA-256>, outside <root>/v1, so
  a build whose scans know nothing of chunks never removes one. The record's file is a manifest
  (M2VCHNK1, length, count, each chunk's SHA-256 and length, its own SHA-256); a read joins the
  chunks and the record's decoder checks it end to end as before (a driver record's checksum, a
  library's content hash). A missing or short chunk is a miss; the next write brings it back,
  and a chunk whose bytes are not its digest's is written again. Smaller records stay whole.
- Crash-safe: every chunk (temporary + rename) before the manifest's rename. An advisory lock
  (std File::lock, <root>/chunks/lock): writers hold it shared from their first chunk look to
  their manifest's rename; the cleanup takes it exclusively with try_lock and removes only
  chunks no manifest names. Neither waits: a writer that meets a cleanup writes its record
  whole.
- Bounded cleanup, in the scans the store makes already: the sweep at device creation where no
  cleanup ran yet or the chunks grew past 110% of what the last kept, and after a cap scan that
  freed chunks. It reads each content record's first 8 bytes (a manifest whole) and lists the
  chunks once. The chunks count against the cap; a removed manifest frees the chunks only it
  named.
- Upgrade: driver::VERSION 3 -> 4, its frame's magic M2VPPLC -> M2VPPL4 (an older build names
  and frames otherwise, never misreads one). The old records are removed by
  Store::sweep -> Chunks::due (no state yet) -> Chunks::cleanup -> Chunks::manifests, which
  removes each content record whose magic is in driver::RETIRED. Each family pays one cold
  making after the upgrade. The validation lanes read the new magic.
- Linux reads: every chunk opened first and posix_fadvise(WILLNEED) on each, so cold chunks
  are read together.

Tests (tests::shader_cache_chunks, 8): round trip and the 256 KiB bound; a shared 1.5 MiB run
kept once (the second record wrote 95,310 of 1,580,864 bytes); no manifest when its chunks
cannot be written; a missing or damaged chunk; the sweep removing a synthetic v3 tree (four
M2VPPLC records, one of 2 MiB), unnamed chunks and a dead writer's temporary; a writer's
shared lock putting the cleanup off; a cleanup's lock making a writer write whole; the cap
evicting the older record with its own chunks.

Verified on msi (m3-max-24179, at this tree's code): clippy -D warnings all features, rustdoc,
shader_cache tests 44 passed, the runtime suite 1512 passed.

Load time, release mode on um790 (NVMe), a store load with a SHA-256 decode in both
(m3-max-24178; the SHA-256 of 1.85 MB alone is 0.80 ms), median of 20 cold (every file dropped
from the page cache) and 200 warm:
- 1.7 MB, 40 chunks (NVIDIA p90): cold 1.56 ms whole, 1.47 ms chunked; warm 0.77 / 0.95 ms.
- 2.5 MB, 67 chunks (worst): cold 2.07 / 2.62 ms (+0.55 ms; one of 20 at 12.1 ms); warm
  1.12 / 1.44 ms (+0.32 ms).
- 20 families sharing the section, all cold, in turn: per family median 0.86 ms whole,
  1.13 ms chunked (first 0.94 / 1.65 ms); whole's slowest 138.6 ms, chunked's 1.18 ms.
A family's later makings on the device use its live cache and read nothing.
2026-11-12 21:10:10 -08:00
..