Skip to content

Creating a worktree thread on Windows reads the repository's git common dir 4 times and re-probes git worktree add -h: 6 of the 19–20 git commands in createWorktree repeat known answers #40185

Open

Description

@juliusmarminge

What is slow

Starting a thread in a new worktree runs GitVcsDriverCore.createWorktree. Besides the checkout itself, it starts 19–20 git processes. Six of them ask questions whose answers it already has, or that do not change between creations:

  • rev-parse --git-common-dir for the project repository, 4 times.
  • rev-parse --show-toplevel and symbolic-ref --quiet HEAD, only to take the shared-config lock for one git config write.
  • git worktree add -h, to find out whether this Git supports --reason.

On Windows each is a process (40–60 ms), on the path the user waits on before the thread's turn starts.

Measured

  • Windows Server 2025, 4 vCPU, Defender on. Commit 80871270f9, server from source.
  • Seed: 6 clones of this repository (~32k files each), 18 devReplay threads. Project without a remote, on branch feature-4.
  • node src/bin.ts thread start --provider devReplay --model todo-list --worktree HEAD --branch perf-nr-<i> --wait --json <projectId> "task", n=5:
per creation (5/5 runs)
thread start, wall 28.1–29.6 s (median 28.4)
processes started by the server (event 4688) 151–169 (median 161), 60–66 of them conhost
GitManager.createWorktree 19.7–20.5 s, 19–20 git
…git worktree add (needed) 18.3–18.5 s
…update-index --refresh (needed) 0.44–1.05 s
GitVcsDriver.resolveRepositoryPaths.commonDir 4 (128–142 ms total)
GitVcsDriver.resolveRepositoryPaths.worktreeRoot + .currentBranch 2 (85–103 ms, run in parallel)
GitVcsDriver.createWorktree.capabilities (worktree add -h) 1 (43–60 ms)

Callers of the 4 common-dir reads, the same in every run: createWorktree (worktree path identity), releaseWorktreeRegistration (unlock after the add), resolveRepositoryPathsUncached (under writeConfigValue, for branch.<b>.t3-merge-base), and GitVcsDriverCore.invalidateWorktreeCreationCaches (after the creation).

How to measure it again

  1. Check out the commit, vp i. Make a clone of this repository: git clone --local <a t3code checkout> p4, git remote remove origin, git checkout -b feature-4, commit one file.
  2. From apps/server: T3CODE_DEV_SEED=0 node src/bin.ts serve --base-dir <home> --port 4800 --host 127.0.0.1 --dev-url http://localhost:5733; node src/bin.ts project add <p4> --base-dir <home>.
  3. Five times, with a new branch name each time: node src/bin.ts thread start --base-dir <home> --provider devReplay --model todo-list --worktree HEAD --branch perf-<i> --wait --json <projectId> "task".
  4. Copy <home>/userdata/logs/server.trace.ndjson* and run:
// wtreads.cjs <trace.ndjson>...: per GitManager.createWorktree, git processes and the repeated reads with their callers
const fs = require("fs"); const sp = new Map();
for (const f of process.argv.slice(2)) for (const l of fs.readFileSync(f, "utf8").split("\n")) { try { const r = JSON.parse(l); if (r.type === "effect-span") sp.set(r.spanId, r); } catch {} }
const kids = new Map(); for (const s of sp.values()) { const k = kids.get(s.parentSpanId) || []; k.push(s); kids.set(s.parentSpanId, k); }
const desc = (s) => { const o = [], q = [s]; while (q.length) { const x = q.pop(); for (const c of kids.get(x.spanId) || []) { o.push(c); q.push(c); } } return o; };
const st = (s) => Number(BigInt(s.startTimeUnixNano) / 1000000n);
const N = ["GitVcsDriver.resolveRepositoryPaths.commonDir", "GitVcsDriver.resolveRepositoryPaths.worktreeRoot", "GitVcsDriver.resolveRepositoryPaths.currentBranch", "GitVcsDriver.createWorktree.capabilities"];
for (const r of [...sp.values()].filter((s) => s.name === "GitManager.createWorktree").sort((a, b) => st(a) - st(b))) { const d = desc(r);
  console.log(Math.round(r.durationMs) + "ms", "git", d.filter((s) => s.name === "runGitCommand").length, N.map((n) => { const x = d.filter((s) => s.name === n); return n.split(".").pop() + "=" + x.length + "/" + Math.round(x.reduce((t, s) => t + s.durationMs, 0)) + "ms"; }).join(" "),
    "| commonDir callers:", d.filter((s) => s.name === "readGitCommonDir").map((s) => sp.get(s.parentSpanId)?.name).join(",")); }

Where the time goes

apps/server/src/vcs/GitVcsDriverCore.ts:

  • readGitCommonDir runs rev-parse --git-common-dir every time it is called. Inside one creation it is called by createWorktree (for the worktree path hash), releaseWorktreeRegistration, resolveRepositoryPathsUncached and invalidateWorktreeCreationCaches, all for input.cwd. None of them consults repositoryPathsCache. That cache is itself invalidated by the previous creation's invalidateWorktreeCreationCaches, so the writeConfigValue lock also re-resolves the worktree root and current branch it does not use.
  • createWorktree runs ["worktree", "add", "-h"] (GitVcsDriver.createWorktree.capabilities) on every creation with a new branch, to look for --reason. The answer depends only on the Git binary.

Why it is avoidable

The project repository's common directory cannot change during one worktree creation. Whether git worktree add supports --reason does not change while the same Git binary is in use. Each repeat is a separate process that the creation waits for.

What a fix has to show

The same 5 creations, before and after:

  • Git processes under GitManager.createWorktree drop from 19–20 to 13–14: common-dir reads from 4 to 1, the worktree-root and current-branch reads for the config lock to 0, and the capability probe to 0 after the first creation.
  • About 0.2 s less before the turn starts. The 18.3 s git worktree add and the index refresh are needed and unchanged.
  • What stays: cancellation and lock-reason behaviour, the shared-config lock, and the cache invalidation after a creation, all tested as today. A Git without --reason still falls back.

Found by the automated Windows performance sweep on 8087127.


Found by the automated Windows performance sweep on 8087127.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions