Repository navigation
Description
Summary
Building a timeline costs time quadratic in a fileChange item's file count. matchFileRows runs for every fileChange item event (started, updated, completed). For each change it scans all of the record's earlier file rows with findIndex + some + includes. A 2,000-file change takes 28 ms per timeline build instead of about 3 ms. A 4,000-file change takes 127 ms instead of about 6 ms. The server runs this build synchronously when it serves a thread timeline (timeline.ts#L1966), so the time blocks the event loop.
Versions and environment
mainat df9619d8e8, run from source.- Linux 6.17, Node 24.14.1.
- Found in a code survey of per-event costs. The steps below are self-contained and need no provider.
Steps to reproduce
Save this script as packages/thread-view/bench.ts in a checkout, then run pnpm install and pnpm exec tsx packages/thread-view/bench.ts from the repo root. It builds a timeline from turn/started, then item/started and item/completed for one fileChange item with N changes, and prints the best of 5 builds after a warm-up for each N.
import { turnScope } from "@bb/domain";
import { EMPTY_ACCEPTED_CLIENT_REQUEST_CONTEXT } from "./src/accepted-client-request-context.js";
import { buildThreadTimelineFromEvents } from "./src/index.js";
const changes = (n: number) => Array.from({ length: n }, (_, i) => ({ path: `src/dir-${i % 37}/file-${i}.ts`, kind: "update" as const }));
const ev = (seq: number, type: string, item?: object): any => ({ event: { type, threadId: "thread-1", providerThreadId: "p", scope: turnScope("turn-1"), ...(item ? { item } : {}) }, meta: { id: `e${seq}`, seq, createdAt: seq } });
const fc = (n: number, status: string, rev = false) => ({ type: "fileChange", id: "fc-1", changes: rev ? changes(n).reverse() : changes(n), status, approvalStatus: null });
const build = (events: any[]) => buildThreadTimelineFromEvents({ acceptedClientRequestContext: EMPTY_ACCEPTED_CLIENT_REQUEST_CONTEXT, contextWindowEvents: [], events, options: { completedTurnDisplay: "collapse", includeNestedRows: true, omitSettledDelegationChildRows: false, includeDiagnosticOperations: false, summarizeShellCommands: true, isLatestPage: true, threadStatus: "idle", threadName: "", workspaceRoot: null } } as any);
const out: any[] = [];
for (const n of [50, 200, 500, 1000, 2000, 4000]) {
const events = [ev(0, "turn/started"), ev(1, "item/started", fc(n, "pending")), ev(2, "item/completed", fc(n, "completed"))];
build(events);
let best = Infinity;
for (let i = 0; i < 5; i++) { const t0 = performance.now(); build(events); best = Math.min(best, performance.now() - t0); }
out.push({ files: n, ms: +best.toFixed(2) });
}
console.log(JSON.stringify(out));Expected vs actual
Expected: build time grows roughly linearly with N.
Actual: build time grows quadratically with N. The table uses the same change order in both events; reversing the order in item/completed costs more, since each lookup then scans past more claimed rows.
| files | build (ms) |
|---|---|
| 50 | 0.67 |
| 200 | 1.46 |
| 500 | 4.24 |
| 1,000 | 8.56 |
| 2,000 | 28.21 |
| 4,000 | 127.36 |
Evidence
- The scan over every earlier row, once per change: item-records.ts#L409-L449.
- It runs for every fileChange item event, through
applyItem→matchFileRows. - Timings:
performance.now()aroundbuildThreadTimelineFromEvents, best of 5 runs after a warm-up, Node 24.14.1.
What you ruled out
- Typical edits touch only a few files. The cost matters for large edits: codemods, formatter runs, renames across a tree, or a generated directory.
- Not a duplicate: no open or closed issue or PR covers
matchFileRows. Prune duplicated command output and started file-change diffs #7806 touched file-change diffs, not row matching. - The matching rule has to stay as it is: each change takes the earliest unclaimed row that shares its
pathormovePath. Row identity across events depends on that rule.
Suggested priority and effort
- Low priority: it costs time only on large file changes.
- Small effort. Index the existing rows by match key once per event, and keep a cursor per key past claimed rows. That gives the same earliest-unclaimed-row result in O(N).
AGENT GENERATED
Activity
bb-slop-cop commentedon Nov 19, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotthreads,perf.confirmed-repro.