Skip to content

refactor(state): wrap state.dep_graph in ArcSwap — foundation for #640 - #642

Merged
zackees merged 1 commit into
mainfrom
refactor/dep-graph-arcswap-640
Jun 4, 2026
Merged

zackees merged 1 commit into
mainfrom
refactor/dep-graph-arcswap-640

Conversation

@zackees

@zackees zackees commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Pure refactor; no behaviour change. Lifts the Arc::get_mut-based write-once constraint on DaemonServer::set_dep_graph so 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>.

Site Before After
SharedState::dep_graph field DepGraph ArcSwap<DepGraph>
Init (lifecycle.rs:133) DepGraph::new() ArcSwap::from_pointee(DepGraph::new())
set_dep_graph &mut self, Arc::get_mut(...).expect(...), panics post-run &self, atomic store(Arc::new(graph)), callable from any thread
26 reader sites state.dep_graph.method(...) state.dep_graph.load().method(...)
Multi-call sites in run.rs (eviction at :201, periodic save at :285, final save at :376) &state.dep_graph passed by reference bind guard to local, reuse — single graph generation per logical operation

Why this is safe

DepGraph is already internally concurrent — all mutable state lives behind DashMap (lock-free) or AtomicU64. 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 the SharedState level.

What this does NOT do

This PR is just the type plumbing. It does NOT yet:

  • Defer the depgraph load to a background task
  • Move the named-pipe bind ahead of the load

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 --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
  • PR CI green — particularly wrapper-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

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]>
@zackees
zackees merged commit 67d1533 into main Jun 4, 2026
26 checks passed
@zackees
zackees deleted the refactor/dep-graph-arcswap-640 branch June 4, 2026 03:46
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
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.

1 participant