Skip to content

refactor(wt-perf): cache fixtures in the platform cache dir, not /tmp or target/ - #3542

Merged
max-sixty merged 4 commits into
mainfrom
wt-perf-fixture-paths
Jul 22, 2026
Merged

max-sixty merged 4 commits into
mainfrom
wt-perf-fixture-paths

Conversation

@max-sixty

@max-sixty max-sixty commented Jul 22, 2026 •

Copy link
Copy Markdown
Owner

What

Move wt-perf's benchmark/debug fixture repos out of std::env::temp_dir() and target/bench-repos/ into the per-user cache dir, resolved by a new wt_perf_cache_dir():

  • $WT_PERF_CACHE_DIR if set, else <cache>/wt-perf via etcetera::choose_base_strategy().cache_dir() — ~/.cache/wt-perf (or $XDG_CACHE_HOME/wt-perf) on Linux and macOS. This matches worktrunk's own base-dir convention (src/config/user/path.rs resolves user config through the same XDG-on-macOS strategy).
  • setup <config> fixtures → <cache>/<config> (was temp_dir()/wt-perf-<config>). --path still overrides.
  • Real cloned fixtures (prune-real, the rust-lang/rust clone) → <cache>/bench-repos/ (was target/bench-repos/).
  • In-process throwaway fixtures (create_repo) are unchanged — they keep using tempfile::TempDir.

Why

Both old homes were wrong for a reused fixture (these are given stable names, persist between runs, and are referenced from docs and -C <path> — that's a cache, not a temp file):

  • A fixed name in a shared /tmp (Linux) collides across users: /tmp is sticky (1777), so a second user's setup hits remove_dir_all().unwrap() on a dir they don't own → EPERM → panic. It's also a predictable-path/TOCTOU hazard, and systemd-tmpfiles reaps /tmp per-file at 10 days by max(atime,mtime,ctime) — on a noatime mount an actively-read fixture still loses cold .git/objects/pack/*.pack mid-use → silent git corruption.
  • On macOS, temp_dir() is already /var/folders/.../T (per-user, 0700), so the docs' /tmp/wt-perf-* paths were simply wrong there.
  • target/ is per-worktree (git worktrees don't share it) and wiped by cargo clean, so the ~15 GiB rust clone was re-cloned per worktree — worst for the most expensive fixture, in a worktree-heavy workflow.

The cache dir is per-user (no collision, no TOCTOU), stable and discoverable (docs/-C references still work), shared across worktrees (clone once per machine), and survives cargo clean — matching sccache, cargo, rustup, Go, Bazel, and Hugging Face, which all place large reusable caches there rather than in /tmp.

Migration notes

  • First bench/setup run after this rebuilds the cache under the new location (the old target/bench-repos/ is no longer read). Existing fixtures can be dropped with rm -rf target/bench-repos.
  • .github/workflows/benchmarks.yaml sets WT_PERF_CACHE_DIR and caches $WT_PERF_CACHE_DIR/bench-repos, so the cached path and the path wt-perf writes stay pinned together (no drift if a runner sets $XDG_CACHE_HOME); key unchanged.
  • Docs (benches/CLAUDE.md, src/commands/CLAUDE.md) updated to the new paths, noting that wt-perf setup prints the exact path.

Testing

  • cargo build --workspace --all-targets, cargo clippy --workspace --all-targets --features shell-integration-tests -- -D warnings — clean.
  • cargo test -p wt-perf — passes.
  • Smoke-tested default resolution (~/.cache/wt-perf/… on macOS), $WT_PERF_CACHE_DIR override, and the prune-real --path rejection (now names the resolved cache path).

This was written by Claude Code on behalf of Maximilian

… or target/

`wt-perf setup` fixtures defaulted to a fixed name under `std::env::temp_dir()`,
and the expensive real-repo fixtures lived under `target/bench-repos/`. Both are
the wrong home for a reused fixture:

- A fixed name in a shared `/tmp` (Linux) collides across users — the sticky bit
  makes the destructive setup's `remove_dir_all` panic for a second user — and
  is a predictable-path/TOCTOU hazard. systemd-tmpfiles can also reap a cold
  `.git/objects/pack/*.pack` at 10 days mid-use on `noatime` mounts. On macOS
  `temp_dir()` resolves to `/var/folders/.../T`, so the docs' `/tmp/wt-perf-*`
  paths were already wrong there.
- `target/` is per-worktree (worktrees don't share it) and wiped by
  `cargo clean`, so the ~15 GiB rust clone was re-cloned per worktree.

Route both through `wt_perf_cache_dir()` — `$WT_PERF_CACHE_DIR` or the platform
cache dir (`~/.cache/wt-perf`, `~/Library/Caches/wt-perf`): per-user (no
collision), stable and discoverable (`-C <path>` and doc references still work),
shared across worktrees (clone once per machine), and surviving `cargo clean`.
In-process throwaway fixtures keep using `tempfile::TempDir`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing as a draft — flagging anything that looks worth a quick fix. Mark ready for a full review.

Clean refactor; the rationale in the description is solid and the doc migration is complete (no stale target/bench-repos or /tmp/wt-perf-* references remain, and the in-process tempfile::TempDir fixtures are correctly left alone). Two low-severity notes inline.

Comment thread .github/workflows/benchmarks.yaml Resolved
Comment thread tests/helpers/wt-perf/src/lib.rs Resolved
Use `etcetera::choose_base_strategy().cache_dir()` instead of
`dirs::cache_dir()`, so the fixture cache is `~/.cache/wt-perf` on both Linux
and macOS — matching worktrunk's own base-dir convention (`src/config/user/path.rs`
resolves user config through the same XDG-on-macOS strategy). `dirs::cache_dir()`
would place it under `~/Library/Caches` on macOS instead.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@max-sixty
max-sixty marked this pull request as ready for review July 22, 2026 09:27
Address two footguns tend flagged in review:

- Treat an empty `WT_PERF_CACHE_DIR=` as unset. `var_os` returns `Some("")` for
  an exported-but-empty var, which would give an empty cache root — so
  `setup <config>` would `remove_dir_all` a *relative* `<config>` in the cwd
  instead of a dir under the cache.
- Pin the benchmarks workflow's fixture cache to `WT_PERF_CACHE_DIR` and
  reference the same var from the `actions/cache` path, so the cached path and
  the path wt-perf writes can't drift. wt-perf otherwise computes the root from
  the XDG cache dir, which honors `$XDG_CACHE_HOME` — a future runner setting it
  would silently re-clone the ~15 GiB rust fixture every run.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint check is failing on a real (deterministic) formatting nit from this PR: the new etcetera import in tests/helpers/wt-perf/src/lib.rs is placed after the std:: imports, but rustfmt wants it sorted alphabetically before them. CI's own diff shows exactly this reorder. Inline suggestion below applies it.

The plan check failure is unrelated — a transient crates.io download blip (download of cl/ap/clap failed — curl failed); it'll pass on rerun.

Otherwise this is a clean refactor: routing the cache through choose_base_strategy().cache_dir() now mirrors worktrunk's own base-dir strategy in src/config/user/path.rs (same etcetera 0.11, XDG-on-macOS convention), and the two prior draft-review concerns (empty WT_PERF_CACHE_DIR, CI cache-path drift) are both resolved.

Comment thread tests/helpers/wt-perf/src/lib.rs Resolved
The lint check runs cargo fmt --check, which sorts `etcetera`
alphabetically ahead of the `std::` imports.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@max-sixty
max-sixty merged commit 4121dc9 into main Jul 22, 2026
39 checks passed
@max-sixty
max-sixty deleted the wt-perf-fixture-paths branch July 22, 2026 10:05
max-sixty added a commit that referenced this pull request Jul 22, 2026
… dir (#3547)

## What

Move every `wt-perf` on-disk fixture out of the per-user cache dir and
under the cargo target dir at `<target>/wt-perf/`, resolved by a renamed
`wt_perf_fixture_dir()`:

- `setup <config>` fixtures → `<target>/wt-perf/<config>` (was
`~/.cache/wt-perf/<config>`).
- The rust-lang/rust clone and `prune-real` fixtures →
`<target>/wt-perf/bench-repos/` (was `~/.cache/wt-perf/bench-repos/`).
- `wt_perf_cache_dir()` → `wt_perf_fixture_dir()`. The target dir is
derived from the **running executable's own path** (it lives inside
whichever dir cargo built into — `<target>/debug/wt-perf`,
`<target>/release/deps/<bench>`), so it honors `CARGO_TARGET_DIR`, a
config-file `build.target-dir`, and cargo-llvm-cov's
`target/llvm-cov-target/` — none of which a bare `CARGO_TARGET_DIR` env
read covers. Falls back to `<workspace>/target` if the binary isn't
under a recognizable profile dir. The `etcetera` dependency is dropped.
- The `WT_PERF_CACHE_DIR` env override (and the empty-value guard it
required) is removed; the benchmarks workflow now caches the
deterministic `target/wt-perf/bench-repos` path directly.
- In-process throwaway fixtures (`create_repo`) are unchanged — they
keep using `tempfile::TempDir`.

This reverses the location decision from #3542, which had moved these
into `~/.cache/wt-perf`.

## Why

`target/` is the conventional home for build/test-generated artifacts —
gitignored, reaped by `cargo clean`, and already where criterion writes
(`target/criterion`). Keeping every wt-perf fixture there gives one
predictable location under the repo that `cargo clean` fully resets.
Deriving the target dir from the running binary (rather than assuming
`<workspace>/target`) keeps that property intact even when the target
dir is relocated.

## Tradeoff (accepted)

`target/` is **not** shared across git worktrees (worktrees don't share
it) and is wiped by `cargo clean`. So the ~15 GiB `prune-real` rust
clone re-clones per worktree and after every `cargo clean` — the cost
#3542 avoided by using the cache dir. This is deliberate and documented
on `wt_perf_fixture_dir`; it's cheap for the synthetic `setup` fixtures,
which rebuild in seconds.

## Testing

- `cargo build -p wt-perf --all-targets`, `cargo test -p wt-perf` —
clean.
- New unit test `target_dir_from_exe_finds_cargo_target` covers the
resolution logic (relocated `CARGO_TARGET_DIR`, bench binary under
`release/deps/`, cargo-llvm-cov's nested target, closest-profile-wins,
and the outside-any-target fallback).
- `pre-commit run --all-files` (fmt, clippy, yaml, typos, cargo-lock,
custom hooks) — clean.
- Smoke-tested end-to-end: `setup branches-2` lands at
`<worktree>/target/wt-perf/branches-2` and writes nothing to
`~/.cache/`; a copy of the binary run from a fake `<dir>/debug/wt-perf`
correctly resolves fixtures to `<dir>/wt-perf/`, proving a relocated
target dir is honored.

> _This was written by Claude Code on behalf of Maximilian_

---------

Co-authored-by: Claude Opus 4.8 <[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.

2 participants