Skip to content

perf(core): intern NormalizedPath via Arc<Path>+Arc<str> (closes #652) - #663

Merged
zackees merged 1 commit into
mainfrom
perf/normalizedpath-arc-intern-652
Jun 5, 2026
Merged

zackees merged 1 commit into
mainfrom
perf/normalizedpath-arc-intern-652

Conversation

@zackees

@zackees zackees commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Switch NormalizedPath internal storage from PathBuf + String to Arc<Path> + Arc<str>. Clone becomes two atomic refcount bumps instead of two heap allocations.
  • Adds normalized_path_clone_is_cheap_post_arc_intern smoke test to guard against regression.
  • Public API unchanged — every accessor returns &Path / &str, so the swap is fully source-compatible workspace-wide (workspace cargo check + clippy clean with zero call-site edits).

Why

From #605 iter T2/T3: depgraph_update_ns averages ~1.8 ms / cold-miss, dominated by cloning NormalizedPath ~600 times per 600-header C++ TU when building file_hashes. Each clone pre-#652 = 2 heap allocations (PathBuf + String); post-#652 = 2 atomic refcount bumps — ballpark 40–100× cheaper per clone.

Pass 2a in the ledger (#605) was originally blocked because entry.last_file_hashes stores the owned Vec for invalidation logic, so the clones can't be replaced with & without restructuring. Arc-interning sidesteps the restructure: the storage still holds owned values, but cloning is no longer alloc-heavy.

Trade-off

The single material cost is into_path_buf(self) -> PathBuf, which is no longer a free move and now allocates. The 10 callers in the workspace are all in cold paths (cli, download_client, watcher) — verified by grep. Zero callers in the daemon hot path.

Other accessors (as_path, case_key, Deref<Path>, AsRef<Path>, AsRef<OsStr>, Serde, Hash/Eq/Ord, join, all From impls) are unchanged in shape and behavior.

Acceptance criteria (from #652)

  • Internal storage switched to Arc<Path> + Arc<str>
  • All reach files compile + tests pass without Arc::make_mut
  • Smoke test asserts NormalizedPath::clone() budget consistent with refcount-bump cost (not heap allocation)
  • perf_meson_probe_storm / latest.json validation — deferred to perf-rust-cluster after merge

Test plan

  • soldr cargo test -p zccache --lib core::path — 14 passed
  • soldr cargo check --workspace --all-targets
  • soldr cargo clippy --workspace --all-targets -- -D warnings
  • soldr cargo fmt --all --check
  • ./test — one pre-existing flake (rustc_multi_output_hit_survives_cache_restore_without_target_dir) fails identically on main; unrelated to this PR
  • perf-rust-cluster bench delta on cpp-inline / Single-file, Cold (target ≥ 500 µs median improvement in depgraph_update_ns)

Closes #652. Refs #605.

🤖 Generated with Claude Code

Switch the internal storage of `NormalizedPath` from `PathBuf` +
`String` to `Arc<Path>` + `Arc<str>`. `Clone` now performs two
atomic refcount bumps instead of two heap allocations, attacking
the per-cold-miss heap pressure measured in #605 iter T2/T3 where
`depgraph_update_ns` spends ~1.8 ms (mean) building `file_hashes`
by cloning `NormalizedPath` ~600 times per 600-header C++ TU.

The public API is unchanged — every accessor already returns
`&Path` or `&str`, so the swap is fully source-compatible. The
single material cost is `into_path_buf(self)`, which is no longer
a free move and now allocates a fresh `PathBuf`. The 10 callers
in the workspace are all in cold paths (cli, download_client,
watcher); zero in the daemon hot path.

Adds `normalized_path_clone_is_cheap_post_arc_intern` smoke test
to guard against regression to per-clone allocation. All 14 path
tests pass; workspace `cargo check`/`clippy` clean.

Refs #605 (cold-path-optimization ledger), DD-005.
@zackees
zackees merged commit 525b191 into main Jun 5, 2026
28 checks passed
zackees added a commit that referenced this pull request Jun 5, 2026
…loses #610) (#665)

Plumbs the registry scaffolded in PR #664 through both ends of
the cold-miss publishing lifecycle, lifting the `#[allow(dead_code)]`
annotation. With this PR landed, **all five DD-025 conditions for
Pass 2 are satisfied**:

| Condition | Status before | Status after |
|---|---|---|
| 1. Named sync point | scaffold only | wired in `store_*` + `try_fast_hit` |
| 2. Failure = miss, never wrong-hit | docs/tests | enforced: all spawn paths call `complete` |
| 3. Blast-radius bound | 5 ms timeout cap in helper | observed by `try_fast_hit` callers |
| 4. Adversarial tests | 6/6 landed | existing 5 + notify-timeout + race coverage |
| 5. Bench-measurable win | not yet exercised | now exercised; perf-cluster validates post-merge |

## Write side — `store_single_output` and `store_rustc_outputs`

Both cold-miss persist spawns now bracket their `spawn_blocking` +
`index_writer_tx.send` work with `register` (synchronously, before
the spawn) and `complete` (inside the spawn, on *every* exit path
including `JoinError`). The completion-on-failure path satisfies
DD-025 condition 2: waiters wake up, re-attempt the lookup, miss,
and recompile — the artifact never returns wrong content.

## Read side — `try_fast_hit`

The fast-hit path becomes `async`. After snapping the
`fast_hit_cache` entry's fields into local clones (so no DashMap
shard guard straddles the await), it calls
`await_pending(artifact_key_hex, PENDING_WAIT_TIMEOUT)` before
materializing. When a cold-miss for the same key is still
publishing, the read path waits ≤ 5 ms instead of falling through
to a wasteful recompile.

`pipeline.rs` adds `.await` to its single `try_fast_hit` call site.
No other cascade is required: `try_request_cache_hit`,
`try_depgraph_cached_hit`, and `materialize_cached_compile_hit`
stay sync — `try_fast_hit` is the only ContextKey-keyed lookup
that benefits from the wait, and it's already on an async caller.

## Why the await is positioned where it is

Cross-shard deadlock avoidance: DashMap's `get` returns a shard
guard. The previous code held that guard across the freshness
checks and then materialized while it was live. With the await
introduced, we now snap `(artifact_key_hex, clock, cached_at)`
into local clones BEFORE the `.await` so no shard lock can
straddle a yield point. The clones are cheap post-#652 (PR #663)
because `artifact_key_hex: String` is small and the other two
fields are `Copy`.

## What this PR explicitly does NOT do

- Does not yet defer `dep_graph.update`. The 1.8 ms median measured
  in #605 iter T2 is still on the response path. Deferring it
  requires moving the entire `store_miss_artifact` body into a
  spawn, which is a larger restructure tracked separately (the
  registry, lookup wiring, and DD-025 ceremony in this PR
  make that follow-up safe to attempt).
- Does not add Loom/TSAN harness — the existing adversarial test
  suite + the cross-platform CI test matrix is the regression bar.

## Test plan

- [x] `soldr cargo test -p zccache --lib` — 1703 / 1703 pass
- [x] `soldr cargo test -p zccache --lib -- pending` — 22 / 22
- [x] `soldr cargo check --workspace --all-targets`
- [x] `soldr cargo clippy --workspace --all-targets -- -D warnings`
- [x] `soldr cargo fmt --all --check`
- [ ] Linux: `deferred_cold_path.rs` adversarial suite (cfg(unix), runs in CI)
- [ ] perf-rust-cluster: validate non-regression on cold scenarios

Closes #610. Refs #605, #664. DD-025.
@zackees
zackees deleted the perf/normalizedpath-arc-intern-652 branch November 5, 2026 05:56
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.

perf(depgraph): intern NormalizedPath via Arc<Path>/Arc<str> to eliminate ~600 per-miss clones (refs #605 iter T3)

1 participant