Skip to content

The Changes preview runs its tracked-file diff twice on every load: the complete stage repeats all 9 git commands of the tracked-only stage (1.4 s of diff stat on 799 files, Windows) #40183

Closed

Description

@juliusmarminge

What is slow

The Changes totals (thread details panel and Diff panel, review.getDiffPreview with metadataOnly: true and sourceKind: "branch-range") load in two stages. First a trackedOnly: true request returns totals for tracked files quickly. Then a second request returns the complete preview, including untracked files. On the server the second request starts from scratch. It resolves the branch, the base and the merge base again, and runs the same tracked diff stat over every changed file again, before adding the untracked listing. With 799 changed files on Windows the repeated stat takes as long as the first one, so the complete totals arrive about twice as late as the new work requires.

Measured

  • Windows Server 2025, 4 vCPU, Defender on. Commit 80871270f9, server from source, production web build, headless Chromium.
  • Seed: 6 clones of this repository (~32k files each), 18 devReplay threads. In one project (no remote, branch feature-3, no untracked files), 799 tracked .ts files modified.
  • Opening a thread in that project and opening and closing its Diff panel, 30 loads. Server trace over all of them:
value
tracked-only previews (9 git each) 55
…followed within 200 ms by a complete preview (10 git) 53
git commands in the complete preview that the tracked-only preview just ran 9 of 10
tracked-only request median 1,019 ms (459–2,604)
complete request median 1,395 ms (839–2,995)
tracked diff stat (getReviewDiffPreview.stat), tracked-only stage median 1,420 ms (314–2,826)
the same stat again, complete stage median 1,412 ms (317–3,016)
untracked listing (review.listUntracked), the only new work median 435 ms (383–800)
branch + merge base again (review.currentBranch, resolveReviewMergeBase) median 85 ms
base-branch lookup again (resolveBaseBranchForNoUpstream; #40125's) median 223 ms

(The stat medians are summed per request across the parallel dirty/base stats, so they can exceed the request time.)

How to measure it again

Setup as in the steps below, then run the driver and the trace script.

  1. Check out the commit, vp i, cd apps/web && vp build.
  2. Make a clone of this repository with git clone --local <a t3code checkout> p3, git remote remove origin, git checkout -b feature-3, and commit one new file. Then modify 800 tracked TypeScript files (PowerShell): $f = git -C p3 ls-files -- '*.ts' | Select -First 800; foreach($x in $f){ Add-Content -LiteralPath (Join-Path p3 $x) -Value '// edit' }.
  3. From apps/server: start T3CODE_DEV_SEED=0 node src/bin.ts serve --base-dir <home> --port 4800 --host 127.0.0.1 --dev-url http://localhost:5733, then node src/bin.ts project add <p3> --base-dir <home> and node src/bin.ts thread start --base-dir <home> --provider devReplay --model todo-list --wait --json <projectId> "task 3-0". Restart the server without --dev-url.
  4. Pair (node src/bin.ts pair --ttl 1h --base-dir <home>, host set to 127.0.0.1:4800) and open the thread in Chromium, or open the Diff panel (Ctrl+D) a few times. Any client that opens the thread works. The driver diff.cjs from the per-file patch issue of the same sweep does it 12 times.
  5. Copy <home>/userdata/logs/server.trace.ndjson* and run:
// pairs.cjs <trace.ndjson>...: tracked-only preview (9 git) followed by the complete preview (10 git), and the time each spends in the same steps
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 en = (s) => st(s) + s.durationMs;
const top = [...sp.values()].filter((s) => s.name === "ws.rpc.review.getDiffPreview").sort((a, b) => st(a) - st(b)).map((s) => ({ s, d: desc(s) })).map((o) => ({ ...o, git: o.d.filter((x) => x.name === "runGitCommand").length }));
const sum = (d, n) => Math.round(d.filter((x) => x.name === n).reduce((t, x) => t + x.durationMs, 0));
for (const a of top.filter((o) => o.git === 9)) { const b = top.find((o) => o.git === 10 && st(o.s) >= en(a.s) - 5 && st(o.s) - en(a.s) < 200); if (!b) continue;
  console.log(JSON.stringify({ trackedMs: Math.round(a.s.durationMs), completeMs: Math.round(b.s.durationMs), statTracked: sum(a.d, "GitVcsDriver.getReviewDiffPreview.stat"), statComplete: sum(b.d, "GitVcsDriver.getReviewDiffPreview.stat"), untracked: sum(b.d, "GitVcsDriver.review.listUntracked") })); }

Where the time goes

  • Client: packages/client-runtime/src/state/review.ts, the diffPreview query's execute. When there is no complete preview yet, it first requests { ...input, trackedOnly: true }, emits that, and then requests input again in full. Two independent RPCs.
  • Server: apps/server/src/vcs/GitVcsDriverCore.ts, getReviewDiffPreview. Both requests run resolveReviewRepository (review.currentBranch), resolveReviewMergeBase (base lookup plus merge-base), and readTrackedDiff → readStats (diff --find-renames … --raw --numstat -z <mergeBase> --) for the dirty and base refs. The complete request also runs prepareUntrackedReviewIndex (ls-files --others --exclude-standard). With no untracked files that returns no temporary index, so its stat is the same command on the same tree as the tracked-only stat.

Why it is avoidable

The complete preview differs from the tracked-only one only by the untracked files. Everything else (branch, base, merge base, the stat of every tracked change) was computed by the request that answered a few milliseconds earlier, and is computed again in full.

What a fix has to show

The same loads, before and after, on the same seed:


Found by the automated Windows performance sweep on 8087127.


Found by the automated Windows performance sweep on 8087127.

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