Skip to content

perf(cargo): each worktree builds into its own slot of the shared target - #316

Merged
bfcarrasco merged 1 commit into
mainfrom
perf/cargo-worktree-artifacts
Dec 4, 2026
Merged

bfcarrasco merged 1 commit into
mainfrom
perf/cargo-worktree-artifacts

Conversation

@bfcarrasco

Copy link
Copy Markdown
Owner

Request

From the owner, after a review of two days of agent sessions on a Rust workspace: "ja lav punkt 1", where point 1 was: give every worktree its own files for the app code in the shared build dir, so a worktree does not rebuild the app code each time another one has built.

Why

Worktrees of one workspace get the same build hashes in a shared target dir. The lock therefore marked a worktree's sources as changed whenever another worktree had built last, so with two or three agents taking turns, nearly every cargo command rebuilt the workspace crates under the lock. On the workspace this was found on (two crates, about 68,000 lines) that is about 105 s per turn, and agents queued behind it for up to 44 minutes.

What changed

  • Each worktree builds its workspace crates into one of six build slots, next to the dependencies all of them share. Cargo names a workspace member's artifacts after the path of RUSTC_WORKSPACE_WRAPPER too; every slot has a pass-through wrapper at its own path, compiled once per target dir with rustc.
  • cargo clippy sets that variable itself, so it gets the slot as --cfg=bstack_slot_<n>. Clippy records its arguments with its result, so another slot's result is never reused, and the test artifacts are left alone.
  • Sources are marked as changed only when a worktree takes a slot (the first time, or from the worktree unused longest when all six are in use), and as before for commands that cannot use a slot: other subcommands, a wrapper of the user's own, no workspace, or no rustc.
  • The lock itself is unchanged: build and run still happen under it.

Removed tests

risk-tier reports "test removed" for two tests that asserted the old behaviour, which is what this change replaces:

  • "sources are marked changed only when another worktree built last, and never for cargo fmt" is kept for commands outside the slots (now driven with cargo rustdoc).
  • "a cargo run ... leaves no last builder, so the next locked run marks its sources" is kept for a run with the user's own wrapper.
    Five tests are new: own build per worktree with nothing rebuilt on return, slot takeover, cargo run in a slot, clippy per worktree, and the two above in their new form.

Scope

Not done: running tests without the lock (the built app at debug/<name> is still one path for all worktrees), and a slot per worktree for clippy's own artifacts (clippy re-checks the workspace crates when worktrees take turns).

Blast radius

Every repo that runs cargo through cargo-lock.mjs. The first build of each worktree after the update rebuilds its workspace crates once into its slot. Disk: up to six sets of workspace artifacts per target dir. BSTACK_CARGO_SLOTS changes the number.

Verification

  • node --test "tests/*.test.mjs": 899 tests, 873 pass, 0 fail, 26 skipped. node tools/check-plugin.mjs: ok.
  • On that workspace (two worktrees on the same commit, shared target dir, this branch's cargo-lock.mjs), cargo test --workspace --locked --no-run:
    • first run of A: 113 s, built both workspace crates; first run of B: 104 s, built the same; dependencies reused.
    • A again: 1 s, built nothing. B again: 1 s, built nothing.
    • one area's tests (a filter on one test program) in A, then B: 3 s each, 11 passed.
    • clippy in A, B, A: 12 s, 66 s, 65 s (re-checked on each turn, as described under Scope); the test build in A right after: 1 s, built nothing.

Worktrees of one workspace get the same build hashes in a shared target dir, so the lock marked a worktree's sources as changed whenever another worktree had built last: every switch between worktrees rebuilt the workspace crates. Each worktree now builds them into one of six build slots, next to the dependencies all share. Cargo names a workspace member's artifacts after the path of RUSTC_WORKSPACE_WRAPPER, so every slot has a pass-through wrapper at its own path. Clippy sets that variable itself and gets the slot as a --cfg, which it records with its result. Sources are marked only when a worktree takes a slot, and for commands that cannot use one.
@bfcarrasco

Copy link
Copy Markdown
Owner Author

bstack-judge: PASS+NOTES tier=1 sha=0784365b6cdbc1be8bba9fd940cfec6fc1936194

PR 316 (head 0784365, checked out detached in a temporary worktree of the local bstack clone, since removed) passes. I found no blocking defect and one non-blocking note.

Criteria, taken from the PR's Request and "What changed", and proven by running node --test tests/cargo-lock.test.mjs on real cargo:

  • Own build per worktree: worktrees A and B share one target dir. A and B each compile on first run. Their second runs compile nothing, and src/lib.rs mtimes are unchanged. .bstack-cargo.last is not written. This is test "each worktree keeps its own build...".
  • Slot takeover: with BSTACK_CARGO_SLOTS=1, the worktree that takes the slot prints "build slot 0 goes from X to Y, whose sources are marked as changed" and its sources are touched. Both worktrees then rebuild. This is the slot-takeover test.
  • cargo run in a slot, no lock: outputs are mine/other/mine, no .bstack-cargo.lock is left, and mtimes are unchanged on the second run.
  • Clippy per worktree: the flagged worktree fails with "needless return" even right after the clean worktree passes. Mtimes are unchanged, and --cfg=bstack_slot_<n> is appended after --.
  • Commands outside the slots: cargo rustdoc, and cargo run with the user's own RUSTC_WORKSPACE_WRAPPER, still mark sources only when another worktree built last. cargo fmt never does.
  • Head result: all 18 tests in tests/cargo-lock.test.mjs pass, 0 fail, 0 skipped.
  • New tests fail on base: I put the base cargo-lock.mjs (8003bf5) back under the head tests. The four new slot tests (own build, takeover, cargo run in a slot, clippy) fail. The two rewritten tests (cargo rustdoc, user's wrapper) pass on base, as they should. The script was restored and the worktree was clean.

Weakened checks: the two removed tests are replaced by equivalents that run the old behaviour through cargo rustdoc and a user wrapper. The PR body explains both.

Attack hypotheses I reproduced on fixtures:

  • Target dir and workspace path containing a space: cargo clippy --version, cargo clippy -- -D warnings and cargo build all work. The wrapper is built once, and the slots file is valid JSON.
  • cargo clippy --version: works, because -- and the cfg are appended without harm.
  • Four worktrees running cargo run at once with 2 slots: the slots file stays valid JSON, the slots lock serializes access, and no run crashed. See F1 for the stale result.
  • Corrupt or old slots file: the code reads it with try/catch and falls back to an empty list. I did not run this against a fixture.

Findings: F1 does not block. It needs more concurrent no-lock cargo run worktrees than there are slots, which is abnormal with the default 6. Because cargo run takes no lock, a worktree that takes a slot over can touch its sources while the previous owner's build in that slot is still finishing. The new owner then sees the finished build as fresh and runs the other worktree's app. I saw w3 print "1" instead of "3". The app at debug/<name> is shared across worktrees, which the PR's Scope section already names as not done, so that may be the cause too. I did not separate the two.

[{"id":"F1","title":"Slot takeover during another worktree's in-flight lock-free cargo run can leave the new owner running the old owner's app (needs more concurrent run-worktrees than slots; shared debug/<name> path is already named in Scope)","reproduction":"BSTACK_CARGO_SLOTS=2, four worktrees with src/main.rs printing their number, CARGO_TARGET_DIR shared, run `node plugins/bstack/scripts/cargo-lock.mjs cargo run --offline --quiet` in all four at once","base":"not run","head":"fail","blocks":"no","files":["plugins/bstack/scripts/cargo-lock.mjs"]}]

@bfcarrasco
bfcarrasco merged commit 800b86d into main Dec 4, 2026
4 checks passed
@bfcarrasco
bfcarrasco deleted the perf/cargo-worktree-artifacts branch December 4, 2026 10:49
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