Skip to content

Check HEAD before walking every ref when creating a worktree - #7066

Merged
ymichael merged 1 commit into
mainfrom
bb/rev-list-all-on-every-worktree-create-thr_s36unefz3u
Nov 5, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/rev-list-all-on-every-worktree-create-thr_s36unefz3u

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Every worktree create checks that the source repository has a commit (readGitRepositoryState in packages/environment-provider-host/src/git.ts). It did this with git rev-list --all --max-count=1. Even with --max-count=1, git has to load and date-sort the tip commit of every ref before it can print one. On a checkout with thousands of branches, that is the slowest git call in thread creation, and the caller only needs to know whether any commit exists. This is finding #7 of the Nov 4 hot-path audit.

What changed

  • readGitRepositoryState now runs git rev-parse --verify --quiet HEAD^{commit} first. If HEAD resolves to a commit, it returns has_commits straight away.
  • It falls back to rev-list --all only when HEAD is unborn. That keeps the existing result for an orphan HEAD when other branches have commits.
  • No visible behaviour change. Each state (not_git, no_commits, has_commits) still maps to the same result and the same error.
  • No wire change, so no HOST_DAEMON_PROTOCOL_VERSION bump. No plugin SDK change.

How you verified

Real data: ~/Projects/bb (4,477 local branches, 2,671 remote refs, 123 worktrees, git 2.50.1). I called the real readGitRepositoryState from this branch and compared it with main's function body, run through the same detectGitSource/runGit runner. Runs were interleaved, after one warm-up each.

run load avg main p50 main p90 main max branch p50 branch p90 branch max
n=20 ~27 813 ms 1,042 ms 1,066 ms 36 ms 49 ms 52 ms
n=30 23→38 2,716 ms 11,478 ms 16,073 ms 66 ms 120 ms 154 ms

Both columns include the shared rev-parse --is-inside-work-tree detection spawn. In both runs the branch took one fast spawn where main took a ref walk whose cost grows with load.

Tests:

  • Added packages/environment-provider-host/test/git.test.ts. It covers a plain directory, an empty repository, a commit on HEAD, an unborn HEAD with commits on another branch (the fallback path), and a bare clone.
  • pnpm exec turbo run typecheck lint --filter=bb-environment-provider-host passes.
  • Full suites are left to CI.

AGENT GENERATED

🤖 Generated with Claude Code

readGitRepositoryState ran `git rev-list --all --max-count=1` on every
worktree create. That sorts the tip commit of every ref, which costs
0.8-2.7 s on a checkout with ~4.5k branches. Verify HEAD first and only
walk all refs when HEAD is unborn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit 4125b2a into main Nov 5, 2026
48 checks passed
@ymichael
ymichael deleted the bb/rev-list-all-on-every-worktree-create-thr_s36unefz3u branch November 5, 2026 00:48
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