Skip to content

Match file-change rows by path index instead of scanning every row - #7834

Closed
cleanunicorn wants to merge 1 commit into
get-bb:mainfrom
cleanunicorn:perf/file-change-row-matching
Closed

cleanunicorn wants to merge 1 commit into
get-bb:mainfrom
cleanunicorn:perf/file-change-row-matching

Conversation

@cleanunicorn

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

matchFileRows (packages/thread-view/src/item-records.ts) matched each change in a fileChange event against the item's earlier rows. It ran record.fileRows.findIndex(...), which checks row.matchKeys.some((key) => matchKeys.includes(key)) for every row. That makes every started, updated or completed fileChange event O(files²). A timeline build with one 2,000-file change took 28 ms, and a 4,000-file change took 127 ms.

What changed

  • matchFileRows maps each match key to the indexes of the rows that existed before the event, once per event.
  • For each change it takes the lowest unclaimed index across the change's keys. A cursor per key moves past claimed rows, so the whole event is O(files).
  • The rule is the same as before: each change takes the earliest unclaimed row that shares its path or movePath. Rows created during the event are claimed when created, and a row's matchKeys change only once it is claimed, so building the index once per event sees the same candidates as the old scan.
  • No wire, CLI or doc changes.
files before (ms) after (ms)
50 0.67 0.67
200 1.46 0.77
500 4.24 1.82
1,000 8.56 2.63
2,000 28.21 3.13
4,000 127.36 6.11

Each figure is one buildThreadTimelineFromEvents call over turn/started, item/started and item/completed for one fileChange item: best of 5 after a warm-up, Node 24, Linux.

How you verified

  • New test, matches each later file change to the earliest unclaimed row sharing a path. It covers duplicate paths, a movePath that matches an earlier row, reordering, and new files. It passes before and after, so the matching rule is unchanged.
  • New test, matches a large file change's rows in time linear in its file count. It is a CPU-time ratio guard in the style of tool-call-parsing-performance.test.ts, comparing 500 and 4,000 files. It fails before the change (expected 337609 to be less than 167280) and passes after.
  • pnpm exec turbo run typecheck lint test --filter=@bb/thread-view: 544 tests pass.
  • A throwaway randomized differential check of the old and new matchFileRows over 200,000 event sequences found no difference in fileRowIndexes, row key or matchKeys. The sequences mixed duplicate paths, movePath renames, reorders, and added or dropped files.
  • The CPU-ratio guard measured 7.9 to 9 after the change over 20 runs (8 is linear). The threshold is 24, and the old code measures about 48.
  • In apps/server, the timeline suites that build through this projection pass: 4 files ran, 68 tests passed. These include timeline-event-budget, timeline-in-turn-window and test/timeline-history. 5 files skipped: the provider-corpus suites need a private corpus (BB_PROVIDER_CORPUS_*), which I don't have.

Fixes #7833

🤖 Generated with Claude Code

AGENT GENERATED

matchFileRows searched all of a record's file rows for every change in a
fileChange event, so each started/updated/completed event cost O(files^2).
Index the existing rows by match key once per event and take the earliest
unclaimed row per key, which keeps the same earliest-row rule in O(files).

A timeline build with one 2,000-file change drops from 28 ms to 3 ms, and
a 4,000-file change from 127 ms to 6 ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-approval PR author is not on the approved contributor list label Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the pull request, @cleanunicorn. It was closed automatically because it does not meet the requirements below yet. This is an automated check, not a review of the change.

Contributor approval. bb reviews pull requests only from approved contributors, and you are not on the list yet. Join the #contributors channel in Discord (https://discord.gg/kvBU6tJhcJ), post a link to your pull request, and explain what it changes and why. A maintainer then adds you to the list and reopens the pull request.

See CONTRIBUTING.md for details.

@github-actions github-actions Bot closed this Nov 19, 2026
ymichael added a commit that referenced this pull request Nov 19, 2026
🚨 **SLOP COP** 🚨 · `new-issue-autopilot`

## Human comments

## What was wrong

Timeline projection searched every accumulated file row separately for
each incoming file change, including rows already claimed in the same
event. This made large file-change events quadratic in their file count.
The behavior reproduces on trusted main in two clean checkouts: a
sixteenfold increase in file count costs about 183× as much with
reversed completion order.

Reproduction and root-cause report:
https://get-bb.github.io/reports/issues/7833.html

## What changed

- Index existing row positions by historical path once per event, with
descending candidate lists that lazily discard claimed rows.
- Select the earliest eligible row across both incoming paths,
preserving historical aliases, duplicate paths, stale-row reappearance,
and the rule that a new row cannot be claimed again in the same event.
- Add a public timeline scaling regression and an identity regression
for competing rename paths and duplicate files.
- All changes stay in `packages/thread-view`; no dependency, lockfile,
generated-file, persisted-data, public API, protocol, CLI, or UI
changes.
- Changed-line count: **193 total text lines across 3 files** (188
additions, 5 deletions), measured with `git diff --numstat origin/main`.
Production: 27 additions and 5 deletions; tests: 161 additions. No
binary changes.

## How you verified

- Added the scaling regression before changing production code. On
unchanged main, its reversed-order case fails: ratio **183.44**,
expected below **64**. A second clean checkout fails the same assertion
at **182.13**. The unchanged-order control passes the intentionally
generous threshold.
- After the fix, the focused scaling cases pass at **8.11×** and
**19.18×** for a sixteenfold file-count increase; 4,096-file builds take
approximately **13–15 ms**, versus **146–401 ms** before.
- The identity regression passes before and after the fix.
- `pnpm exec turbo run test --filter=@bb/thread-view --
test/file-change-scaling.test.ts test/build-thread-timeline.test.ts
--silent=false`: **86 passed**.
- `pnpm exec turbo run test typecheck lint:type-aware
--filter=@bb/thread-view`: **545 tests passed**, typecheck and
type-aware lint passed.
- `pnpm exec turbo run test --filter=@bb/server -- test/timeline-history
test/services/threads/timeline-in-turn-window.test.ts
test/services/threads/timeline-event-budget.test.ts
test/public/public-thread-timeline-work-rows.test.ts`: **58 passed, 2
skipped**. The history-replay cases require `BB_TIMELINE_HISTORY_DB`,
which was not supplied; no real application data was used.
- `pnpm exec oxfmt` on the three changed files and `git diff --check`
pass.
- PR #7834 is closed and unmerged; its branch was not checked out or
run. No linked open PR existed when this change was prepared.

Fixes #7833

> AGENT GENERATED

<!-- slopcop:rule=new-issue-autopilot run=run_6gq8iyw8mwno7ead
sha=issue-7833 kind=summary -->

---------

Co-authored-by: SawyerHood <[email protected]>
Co-authored-by: Michael Yong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-approval PR author is not on the approved contributor list

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timeline builds take quadratic time in a fileChange item's file count

1 participant