Skip to content

perf: gc reports ask git each question once - #6627

Merged
janicduplessis merged 1 commit into
mainfrom
perf/6615-gc-git-calls
Nov 28, 2026
Merged

janicduplessis merged 1 commit into
mainfrom
perf/6615-gc-git-calls

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Description

On the maintainer's MacBook (258 registered projects), stim gc --dry-run --json runs about 6,600 git commands, and about 4,200 of them have the same folder and arguments as an earlier one (#6615). The cause is one call: the dependency pass lists worktrees through artifactWorktreesFor, which always drops the run's shared git reads (forgetGitReads), even in a report where nothing acts. That drop sits between the plan's worktree sweep and the report's second sweep (collectGcReport), so the second sweep reads every checkout again. Merge state (patch-id, log -p, diff) also never went through the shared reads, and concurrent sweeps of one repository each ran their own git worktree list.

Maintainer decision: "can scan like stream in, and is it possible to further reduce git calls", 2026-11-27 (#6615 comment). The streaming half is #6613 and is not in this PR.

Solution

Everything stays in the git-read layer and its call sites. The report, cache and streaming code that #6612 and #6613 change is not touched.

  • A report keeps its reads. The dependency pass lists worktrees (and so drops the shared reads) only when it acts; a report never used that list. Acting passes behave as before: the reads are dropped before and after every action and under every workspace lock, so whatever a deletion relies on is still read fresh at delete time.
  • Merge state is shared, keyed on what it depends on. checkMergeStates keys each worktree's merge state on its HEAD, its branch and the commit the default branch points to. That commit is read fresh with one rev-parse per repository per sweep, so a fetch or a moved origin/main gets a new answer. The origin/HEAD lookup, the removal holds (registeredRemovalHold, discoveredRemovalHold) and uncommittedActivityOf are shared per sweep through the existing sweep\0... keys. The fresh rechecks under the removal locks (removalUseHold, the normal-observation recheck) are unchanged.
  • One git worktree list per repository. In an open scope, listWorktreesAsync keys the listing on the on-disk common directory, so concurrent sweeps of the stim repository's 38 worktrees ask git once instead of 38 times. The on-disk probe goes through bounded-read.ts (3 s, abortable); an unanswered probe, an empty listing, or a listing that does not contain the checkout falls back to that checkout's own git worktree list.
  • The source walk asks check-ignore once per depth instead of once per folder. scanSourceWrites reads one depth at a time and sends all of that depth's build-named folders in one git check-ignore -z --stdin. git refuses the whole list when one path is inside a submodule (fatal: Pathspec ... is in submodule), so on any failure other than exit 1 the walk asks again folder by folder when more than one folder at that depth had candidates. A refusal costs one extra call per depth over the per-folder walk. A walk now reads a whole depth before it checks entry times, so on an active checkout it can read a few more folders before it stops at the first recent write.

LLP 0001 gains "A report asks git each question once" and LLP 0004's source-scan section describes the per-depth probe.

One case is left out on purpose: a bare repository nested inside another checkout would be keyed on the outer repository's common directory. Normal layouts don't do that, and the worst case is a wrong listing for that run's report.

Not done here: a merge-state cache across runs (stimd reruns the report every 15 minutes, and the CLI is one-shot). The removal reads that make up the remaining per-worktree cost (status --porcelain=v2, log --oneline HEAD --not, rev-parse --git-path modules, ls-files --stage) depend on working-tree files as well as refs, so keying them on HEAD or index mtimes would not be safe.

Test plan

Measured on the maintainer's MacBook (load average 8 to 10) by logging every git child through a PATH shim. The shim hashes stdin so check-ignore and patch-id calls with different input count as different calls:

before (aaa3efa) after
CLI stim gc --dry-run --json 6,617 git calls, 225 s 2,685 git calls, 134 s
API gc({measureBuildState: false}) (Desktop's path in stimd) 6,839 git calls, 256 s 2,701 git calls, 130 s
check-ignore calls (CLI) 1,814 219
worktree list calls (CLI) 356 75
repeated identical calls (CLI) about 4,200 107
  • The before and after CLI payloads give the same verdicts and actions for all 257 actions. The only differences are elapsed-time text and worktrees other agents changed between the two runs.
  • git check-ignore -z --stdin was checked against the real git (2.54): it echoes nested relative paths as given, exits 1 when nothing is ignored, and exits fatally for a path inside a submodule (invariant 9).
  • New tests in git-reads.integration.test.ts cover these cases:
    • worktrees of one repository listed side by side ask git once;
    • a worktree's merge state is read once per run and read again after origin/main moves;
    • a walk asks check-ignore once per depth, then per folder when a submodule fails the batch, and still skips the ignored build/;
    • a listing falls back to git while a stalled read holds the bounded probe slot (this exercises the fallback, not a real stalled volume).

Fixes #6615

@janicduplessis janicduplessis added area: cli packages/stim-cli, including its README and guide ci-full run the macOS, Windows, Desktop and license jobs on this pull request labels Nov 28, 2026
@janicduplessis
janicduplessis marked this pull request as ready for review November 28, 2026 06:48
@janicduplessis janicduplessis added the review: ai-approved A fresh AI review found nothing left to fix label Nov 28, 2026
@janicduplessis
janicduplessis enabled auto-merge (squash) November 28, 2026 06:49
@janicduplessis
janicduplessis merged commit eb523ef into main Nov 28, 2026
71 of 72 checks passed
@janicduplessis
janicduplessis deleted the perf/6615-gc-git-calls branch November 28, 2026 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli packages/stim-cli, including its README and guide ci-full run the macOS, Windows, Desktop and license jobs on this pull request review: ai-approved A fresh AI review found nothing left to fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gc dry run repeats about 4,200 identical git calls per run

1 participant