Repository navigation
refactor(state): wrap state.dep_graph in ArcSwap — foundation for #640 - #642
Merged
Merged
Conversation
Pure refactor; no behaviour change. Lifts the `Arc::get_mut`-based write-once constraint on `DaemonServer::set_dep_graph`, which was the blocker called out in #640 for the actual deferred-depgraph-load that collapses the #637 first-wave herd window. ## What changed `SharedState::dep_graph: DepGraph` → `arc_swap::ArcSwap<DepGraph>`. Initial value: `ArcSwap::from_pointee(DepGraph::new())` (an empty default graph). Subsequent calls to `set_dep_graph` atomically swap in the loaded graph via `.store(Arc::new(graph))`. The swap is safe to call from any thread, at any time during the daemon's lifetime, including from a background task started AFTER `server.run()` — that's the property the follow-up needs. All 26 reader sites in `crates/zccache/src/daemon/server/` (`connection.rs`, `handle_clear.rs`, `handle_compile/error_cache.rs`, `handle_compile/miss_store.rs`, `handle_compile/pipeline.rs`, `handle_compile_multi.rs`, `keys.rs`, `run.rs`, `util.rs`) updated to `state.dep_graph.load().method(...)`. The `Guard<Arc<DepGraph>>` returned by `.load()` derefs through `Arc<DepGraph>` to `&DepGraph`, so each method call is a one-line change with no semantic difference. Where the prior code held an `&DepGraph` reference across multiple method calls (eviction in `run.rs:201`; periodic + final depgraph saves at `run.rs:285,376`), the new code binds the load guard to a local first and reuses it — so a hypothetical concurrent swap can't split a logical operation across two graph generations. ## Why this is safe DepGraph is already internally concurrent — all its mutable state lives behind `DashMap` (lock-free) or `AtomicU64`. So every method takes `&self`, not `&mut self`. The ArcSwap wrap doesn't change that; it adds atomic replacement of the WHOLE graph at the `SharedState` level, orthogonal to the per-entry mutations. `set_dep_graph` previously panicked if called after `run()` started (via the `Arc::get_mut(...).expect(...)` pattern). That panic was a correctness guard; this refactor removes the underlying need for the guard (the swap is atomic). ## What this does NOT do This PR is just the type plumbing. It does NOT yet defer the depgraph load to a background task, and it does NOT yet move the named-pipe bind ahead of the load. Those are the actual herd-collapse changes; they ride on top of this foundation in a follow-up PR. That split keeps each PR small and individually testable — the prior monolithic attempt (v1 of #639) broke the wrapper-e2e Windows smoke lane. ## Verification - `soldr cargo check -p zccache --tests` clean - `soldr cargo clippy -p zccache --tests -- -D warnings` clean - `soldr cargo fmt --all -- --check` clean - `soldr cargo test -p zccache --lib` — 1688 / 1688 pass The next PR can spawn_blocking the depgraph load and call `server.set_dep_graph(graph)` from the resulting task — exactly the shape #640 sketches. Refs: #640 (foundation; deferred-load follow-up tracks the actual herd-collapse fix) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This was referenced Jun 4, 2026
zackees
added a commit
that referenced
this pull request
Jun 4, 2026
#648) Built on #642's ArcSwap foundation. DaemonServer::bind now happens at t≈0 inside rt.block_on; the depgraph load runs in a background spawn_blocking and installs via a new DepGraphSetter handle when complete. server.run() accept-loop is on the same timeline as before (bind + accept inside the same async block), so the v1 #639 wrapper-e2e regression doesn't recur. First-wave herd from #637 now exits in microseconds at the bind step instead of paying the 3+ s depgraph cost. 1693/1693 lib tests pass, all 30 CI checks green. Closes #640
2 of 7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pure refactor; no behaviour change. Lifts the
Arc::get_mut-based write-once constraint onDaemonServer::set_dep_graphso the follow-up PR can actually defer the depgraph load to a background task. That deferred load is what closes the #637 first-wave herd window, but it depends on this type plumbing landing first.What changed
SharedState::dep_graph: DepGraph→arc_swap::ArcSwap<DepGraph>.SharedState::dep_graphfieldDepGraphArcSwap<DepGraph>lifecycle.rs:133)DepGraph::new()ArcSwap::from_pointee(DepGraph::new())set_dep_graph&mut self,Arc::get_mut(...).expect(...), panics post-run&self, atomicstore(Arc::new(graph)), callable from any threadstate.dep_graph.method(...)state.dep_graph.load().method(...)run.rs(eviction at :201, periodic save at :285, final save at :376)&state.dep_graphpassed by referenceWhy this is safe
DepGraphis already internally concurrent — all mutable state lives behindDashMap(lock-free) orAtomicU64. Every method takes&self, not&mut self. The ArcSwap wrap doesn't change per-entry mutation semantics; it adds atomic replacement of the WHOLE graph at theSharedStatelevel.What this does NOT do
This PR is just the type plumbing. It does NOT yet:
Those are the actual herd-collapse changes; they land in a follow-up PR on top of this foundation. The split keeps each PR small and individually testable — the prior monolithic attempt (v1 of #639) broke the
wrapper-e2e (windows-latest)smoke lane.Test plan
soldr cargo check -p zccache --testscleansoldr cargo clippy -p zccache --tests -- -D warningscleansoldr cargo fmt --all -- --checkcleansoldr cargo test -p zccache --lib— 1688 / 1688 passwrapper-e2e (windows-latest)(no run_server reorder yet, so this should be a no-op behaviour-wise)Refs: #640 (foundation; deferred-load follow-up tracks the actual herd-collapse fix)
🤖 Generated with Claude Code