Skip to content

perf(daemon): in-memory artifact index, WAL flush, pack mode (#274) - #278

Merged
zackees merged 3 commits into
mainfrom
perf/persist-path-issue-274
May 19, 2026
Merged

zackees merged 3 commits into
mainfrom
perf/persist-path-issue-274

Conversation

@zackees

@zackees zackees commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Closes #274.

Summary

  • Persist path now hot-in-memory. A new background WAL writer task drains an in-memory HashMap and snapshots the artifact index to a bincode blob (index.bin) on a 5 s timer / 2048-entry budget. Persist call-sites send + return; the disk-write semaphore no longer blocks on the index commit.
  • Artifact packing (env-gated ZCCACHE_PACK_ARTIFACTS=1). Concatenates a cache miss's payloads into one {key}.pack file. Microbench shows ~2.6× wall-clock improvement at 5 payloads per job — Defender's per-file scan cost is the bottleneck, and pack mode collapses it with file count.
  • Drops redb for the artifact index. Reads were already 100 % in-memory via state.artifacts; redb was only doing per-txn fsync on the write side. The new ArtifactStore is a DashMap snapshotted via temp+rename. kv.rs (separate Kv cli subcommand) keeps its redb usage.
  • Bench harness in crates/zccache-daemon/tests/persist_pool_bench.rs (#[ignore]) compares strategies side-by-side. bench/persist-rust-project/ is a minimal standalone crate (~10 dummy-linked deps) for cold-build end-to-end measurement.

New env vars

Var Default Purpose
ZCCACHE_STORE_WORKERS 8 Persist semaphore size
ZCCACHE_WAL_FLUSH_MS 5000 WAL flush interval
ZCCACHE_WAL_MAX_PENDING 2048 WAL size-based early flush
ZCCACHE_PACK_ARTIFACTS unset Enable per-artifact packing

Bench (Windows 10, Defender on, 16-core)

200 jobs × 5 payloads × 50 MB workload:

  • baseline (in-memory store): 284 ms median
  • packed + async writer (sem=16): 59 ms median

200 jobs × 3 payloads × 327 MB workload (bandwidth-bound):

  • baseline: 219 ms
  • packed: 211 ms (within noise — disk-throughput limited)

Migration notes

Existing users get a fresh cache index on upgrade — old `index.redb` is orphaned and can be deleted manually or via `zccache clear`. Artifact files on disk are intact and re-discovered as misses repopulate the in-memory index. Documented in `index_path_from_cache_dir`.

Test plan

  • `cargo test --workspace --lib` — ~1300 tests pass (13 new in artifact store, 4 new for pack format)
  • `cargo clippy --workspace --all-targets -- -D warnings` — clean
  • `cargo test -p zccache-daemon --test rustc_issue_210_async_populate_test` — passes (exercises real persist + hit path)
  • `cargo test --release -p zccache-daemon --test persist_pool_bench -- --nocapture --ignored` — bench runs
  • CI green on Linux + macOS + Windows
  • Soak in a real-world cold soldr build

🤖 Generated with Claude Code

Closes the persist-path latency gap from issue #274 by reshaping the
write path so both reads and writes stay in memory on the hot path.

Daemon (`crates/zccache-daemon/src/server.rs`):
- `persist_artifact_payloads` consolidates the three persist call-sites
  behind one helper. Behind `ZCCACHE_PACK_ARTIFACTS=1` it concatenates
  every payload of a cache miss into a single `{key}.pack` file with a
  small `ZCPK` header; `ensure_payloads` transparently falls back to
  extracting from the pack when the unpacked layout is absent. On a
  5-payload workload the bench shows ~2.6× wall-clock improvement
  because Defender's per-file scan cost collapses with file count.
- Background `run_index_writer` task drains an in-memory `HashMap` WAL
  and snapshots to disk on a timer (`ZCCACHE_WAL_FLUSH_MS`, default
  5 s) or eagerly at `ZCCACHE_WAL_MAX_PENDING` (default 2048). The
  persist semaphore (`ZCCACHE_STORE_WORKERS`, default 8) no longer
  blocks on the index commit. Shutdown notifies the writer for a
  synchronous final flush bounded by a 2 s timeout.

Artifact store (`crates/zccache-artifact/src/store.rs`):
- Replaces the redb-backed implementation with a `DashMap` snapshotted
  to a bincode blob (`index.bin`) via temp+rename. All mutators are
  now infallible in-memory ops; only `flush()` touches disk. Drops
  per-txn fsync overhead and lets the hot path skip the redb lock
  entirely. Includes 13 unit tests for round-trip, corruption
  tolerance, and atomic replacement.

Index path (`crates/zccache-core/src/config.rs`):
- `index.redb` → `index.bin`. Existing redb files are orphaned on
  upgrade; the daemon rebuilds the index by re-missing affected keys
  (artifact files on disk remain valid).

CLI (`crates/zccache-cli/src/main.rs`):
- `wait_for_redb_release` → `wait_for_daemon_teardown`: with no file
  lock to drop, endpoint reachability is the only signal we need.
  Removed 3 Windows-only tests for redb's exclusive-share semantics.

Bench harness:
- `crates/zccache-daemon/tests/persist_pool_bench.rs` — Rust microbench
  that drives several persist strategies side-by-side over realistic
  synthetic workloads, gated behind `#[ignore]`. Used to pick the
  defaults above.
- `bench/persist-rust-project/` — minimal standalone crate (~10
  dummy-linked deps) for end-to-end cold-build measurement.

Env vars (all documented in `server.rs` doc comments):
- `ZCCACHE_STORE_WORKERS`         (default 8)
- `ZCCACHE_WAL_FLUSH_MS`          (default 5000)
- `ZCCACHE_WAL_MAX_PENDING`       (default 2048)
- `ZCCACHE_PACK_ARTIFACTS`        (default off)

~1300 workspace unit tests pass. `cargo clippy --workspace
--all-targets -- -D warnings` clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees force-pushed the perf/persist-path-issue-274 branch from 3c8ea6c to b98a600 Compare May 19, 2026 06:39
zackees and others added 2 commits May 18, 2026 23:46
The `tests::warm_*` and `tests::adversarial_*` suites construct fake
artifact stores in test temp dirs and pass them to `warm_target`. They
previously hardcoded `index.redb` and relied on `store.insert` writing
synchronously. After dropping redb:

- Hardcoded `index.redb` paths updated to `index.bin`. `cmd_warm` now
  uses `index_path_from_cache_dir` for consistency with the daemon.
- Each `make_test_store` and inline test setup now calls
  `store.flush().unwrap()` before `drop(store)` so the on-disk blob
  exists when `warm_target` opens it. Without this, the tests pass
  locally on Windows (where the file may flush via a different path)
  but fail on Linux CI with "no artifact index at ...".

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Release for the persist-path improvements + index store refactor landed
in this PR (#278): in-memory artifact index with periodic bincode flush,
WAL writer, env-tunable persist semaphore, and pack-mode (env-gated).

Replaces the redb-backed `ArtifactStore` with a `DashMap` snapshotted
atomically to `~/.zccache/index.bin` — a user-visible filename change
(legacy `index.redb` files are left orphaned and can be cleaned via
`zccache clear`).

Merging this commit to main triggers `release-auto.yml`, which detects
the workspace version delta and publishes wheels + crates artifacts.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit 196f5d9 into main May 19, 2026
23 checks passed
@zackees
zackees deleted the perf/persist-path-issue-274 branch May 19, 2026 07:03
zackees added a commit that referenced this pull request May 19, 2026
When `cache-target: "true"` + `target-restore-fallback: "true"` are set,
the action restores a `target/` snapshot from a previous commit that
shared the same `Cargo.lock`. The post-restore step touched *every* file
in `target/` to `now + 1 minute` so cargo's "output mtime > source mtime"
check would treat the restored artifacts as up-to-date.

The problem: that touch also hits `target/debug/.fingerprint/<crate>-*/`,
which contains the dep-info files cargo uses to decide whether to invoke
rustc at all. Future-stamping them makes cargo trust the cached binary
even when the source has changed — silently linking the *previous*
commit's compiled binary against the current source layout. Symptoms
observed in PR #278: identical failures on all three test workflows
(linux/macos/windows) with panic line numbers and error strings matching
the previous commit's source, not the just-pushed source.

Fix: exclude `*/.fingerprint/*` from the stamping pass. Output files
(rlib/rmeta/build artifacts) still get the future stamp so the original
"output newer than source" optimization works for unchanged crates.
Fingerprint dep-info files keep their original mtimes from when the
snapshot was saved — older than newly-checked-out changed sources, so
cargo correctly detects the change and re-invokes rustc.

Verification: after this lands, a contributor pushing a single-line edit
to a test file should see that change reflected in CI test output. The
PR-278 workaround of bumping `Cargo.lock` to bust the restore-key chain
is no longer necessary for source-only changes.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Daemon write-path parallelism: audit, benchmark, improve for Defender-on environments

1 participant