Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): older history pages load without indexing the whole thread #34573

Mergedt3dotgg(GitHub profile, opens in a new tab)merged 9 commits intomain fromrefs/pull/34573/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHub

t3dotgg(GitHub profile, opens in a new tab)commented

Problem

"Load earlier turns" times out on a very long thread. The client gives a history page 6 seconds. Every older history page, every t3_thread_read call and every guest share page first builds an index row for each turn item of the thread, then keeps one page of them.

#24422 keeps that index between pages, but any event on the thread drops it: a running turn, a sync of its pull request, or opening the thread. So the first click after each of those pays for the whole thread again. #35224 made the read cheaper and left it in place. On a 45,154-item thread with 315 MB of payloads it still reads 287 MB from a cold disk for one page.

Change

For a thread that shows no fork ancestry, a page reads only the rows near it:

  • Older history pages find the cursor row by key and read a bounded range of rows below it. The range grows until the page ends inside rows whose inputs are all known, so the selection gives the same page as on the full index. With deferred work, each run that folds work is read whole, because a fold needs the run's stop rows, error rows and group sizes.
  • Timeline pages (t3_thread_read) read from the start position to the page end.
  • Positions and totals come from counts on the covering (thread_id, ordinal, turn_item_id) index. Rows hidden by a rolled-back run, a cancelled queued turn or a replaced attempt are found from their runs and subtracted.

readTimelineIndex and readSnapshotTimelineIndex are the same text as on main. A read that only the full index can answer still builds and caches it, with the page-scoped payload scan of #35224. While that index is fresh, every page of the thread uses it.

Server only. No schema, migration, wire contract or cursor format change. Cursors that clients hold today keep their meaning. Web, desktop and mobile read the same endpoint.

Scope

Maintainer PR. It removes the read that the "Load earlier turns" timeout comes from, which #35224 measured and could not remove, and it closes the cost that #20427 describes.

Verification

Numbers

One thread of 45,154 turn items, 1,926 runs and 315 MB of payloads, cloned from real threads into a copy of a real 9.2 GB database. One fresh process for each read, so no index is cached. "Before #35224" is main at 2482b03a01.

Warm file cache, milliseconds:

ReadBefore #35224mainThis PR
Older page, deferred work (the web and mobile default), newest801 to 834428 to 47480 to 89
Older page, deferred work, middle of the thread791 to 818393 to 488100 to 110
Older page, deferred work, oldest788 to 926437 to 59999 to 122
Older page, work inline267 to 268270 to 30072 to 80
Older page, conversation only773 to 812390 to 41496 to 108
Work group page786 to 807419 to 429101 to 110
t3_thread_read, messages, limit 20260 to 290253 to 29164 to 90
t3_thread_read, activity, limit 20260 to 277269 to 30873 to 78
Jump to an entry (full index)834 to 897473 to 482479 to 517
Cursor on a missing row (full index)803 to 858450 to 464506 to 507
Cursor on a row that deferred work folds (full index)785 to 837768 to 799825 to 833
Fork of that thread, newest page (full index)790 to 807448 to 465449 to 464

Cold file cache (the database file evicted before each read), seconds and megabytes read from disk:

ReadBefore #35224mainThis PR
Older page, deferred work, newest9.6 s, 664 MB5.3 to 6.1 s, 287 MB0.57 to 0.76 s, 29 MB
Older page, deferred work, middle10.6 s, 663 MB5.1 to 6.9 s, 287 MB0.67 to 0.79 s, 38 MB
Older page, work inline3.5 s, 210 MB3.6 to 6.2 s, 210 MB0.50 s, 21 MB
Work group page11.4 s, 664 MB5.7 to 7.1 s, 303 MB0.70 to 0.93 s, 41 MB
t3_thread_read, messages3.4 s, 210 MB3.3 to 3.8 s, 210 MB0.45 to 0.52 s, 21 MB
Jump to an entry (full index)9.5 s, 666 MB5.4 to 5.8 s, 306 MB6.1 to 6.3 s, 306 MB
Cursor on a missing row (full index)665 MB5.2 to 5.8 s, 293 MB5.4 to 6.8 s, 294 MB
Fork, newest page (full index)not measured5.4 to 5.7 s, 287 MB5.7 to 6.3 s, 287 MB

Cold reads on this machine were 4 to 5 times slower in these runs than in the runs for #35224 on the same shape (old main 9.6 s here, 1.8 to 2.3 s there). The megabytes are the stable number: a windowed page reads 21 to 41 MB where main reads 210 to 303 MB.

Same pages as main

Every read was run on main (4fd40e1a16e, with #35224) and on this branch over the same database copy, in one process, and compared as bytes. A failed read is compared down its whole error cause chain. Each read was also run on this branch with the cached-index shortcut removed, so every eligible read takes the window.

Database copyThreadsReadsMismatches
Real database, 9.2 GB3,986 (7 with fork ancestry)1,317,3120
The same, with 489 runs rolled back, 540 cancelled, 643 attempts replaced, 9,039 ordinal ties and 1,145 threads with a running run3,9861,304,9310
Second real database, 2.9 GB, with one 25,536-item thread1,159439,3070
The dev server's database while a Replay turn ran63,1370
The 45,154-item thread of the Numbers section and a fork of it210,8120

3,064,687 reads in the first four rows. 455,760 of them fail (bad cursors) and fail the same way. With the shortcut removed, 1,944,474 of them were answered from a window.

The reads for each thread: every history page from the first cursor back to the start in four client modes (deferred work, work inline, conversation only, conversation only with deferred work), cursors of one mode used in another, every work group page, jumps to an entry, stale and invalid cursors, timeline pages in both views with many afterPosition, limit and itemId values, attachment lookups, and t3_thread_read paged over the whole thread.

Tests

  • Three tests fail on main and pass here. In ProjectionStore.test.ts: the statements of a page cost the same SQLite VM steps after older rows are deleted (expected 144210 to be at most 105963), and one generated test compares window pages with full-index pages (expected +0 to be above 60). In threadHistoryPaging.test.ts: a page selected from the newest part of a history equals the page selected from the whole history.
  • The generated test builds four seeded threads with rolled-back runs, cancelled queued turns, replaced attempts, stop requests, folded work, wake turns, steers and ordinal ties. I broke the window code in 12 ways, one at a time (positions that ignore hidden rows, runs that fold work not read whole, no growth of the window, and so on). The test fails for each of the 12.
  • The payload scan of #35224 now has an assertion on the run ids it binds: a run on the page is scanned and a run after the cursor is not. A variant that scans every run of the thread fails it.
  • vp run check:changed and vp run test:perf:v2-wire pass.

Concurrency

A page read is one reader transaction: sql.withTransaction on the read-only connection, with a deferred BEGIN, so the first statement fixes the snapshot. I committed a write from a second connection before each statement of a windowed read, 760 times: rows appended by a running turn, a rollback of the two newest runs, a compaction row, and a run with folded work that completes. Every page equalled the page of main for one committed state. It was the state after the write only when the write landed before the first statement.

Real server

vp run dev on a seeded copy of real data, in the web client:

  • "Load earlier turns" to the start of a 1,349-item thread: 9 clicks, 11 pages, 776 rows, the same as on main. Its folded work group: 6 pages, 446 rows.
  • A jump from the find bar to a result at the start of a 1,069-item thread: 14 requests, 1,249 rows.
  • A fork thread to the start: 8 pages, 555 rows.
  • With a Replay (dev) turn running: history pages of that thread, and "Load earlier turns" to the start of a long thread.
  • A snapshot of that server's database, taken while the Replay turn ran, gave 3,137 reads with 0 mismatches against main for the six threads used.

Not checked in the running server: a guest share page (dev has no T3 Connect tunnel) and the MCP t3_thread_read tool (no MCP client for the dev server). Both call the reads compared above.

Still on the full index

These take the same read as main, with the same results and the same cost:

  • Threads that show fork ancestry.
  • A jump to a target entry (throughEntryId outside a work group): a search result or a citation link.
  • A cursor that names no visible row of the thread, or a row that deferred work folds away. On the two real databases this is the first "Load earlier turns" click of 57 of 839 and 44 of 674 threads that have an older page: the snapshot's cursor names a row that deferred work folds. Every later click reads a window.
  • Attachment lookups in getTimelinePage, which a guest share page uses for media.
  • Thread search.

On a machine 5.2 times slower than this one, with a cold disk, these can still pass the client's 6 seconds on a thread of this size. A windowed page stays under it.

Costs that remain

  • A windowed page is slower than a page from an index that main has cached: 42 to 83 ms against 5 to 55 ms on the 45,154-item thread. main has that index only until the next event on the thread, and then pays 250 to 600 ms to build it again.
  • Every windowed page reads and decodes every run and attempt of the thread, and counts positions over the thread's index. These read no payloads. They are most of what a small windowed page costs on a cold disk.
  • A deferred page next to a very large folded run reads that run whole.
  • A messages page between two far-apart messages reads the item ids between them.
  • Every row of a rolled-back run is loaded to subtract it from positions.

Reviewed with sol-loop at 5cd86541b6: 3 passes with GPT-6.1 Sol, one open item (a fork read its own thread row twice), fixed here.

Created with Claude Fable 5.2 in Claude Code (T3 Code).

🤖 Generated with Claude Code

    1. Pushed9252e3d

    2. Note

      🤖 Claude Fable 5.2 responding on behalf of Theo

      The open Sol item is fixed, and the branch is merged with main at 4fd40e1a16e. What changed since 5cd86541b6:

      The Sol item: a fork thread read its own thread row twice. Sol was right. The window opener loaded and decoded the thread row to look for fork ancestry, and the full index then loaded it again. One SQL statement now finds a fork, so a fork loads nothing before the full index does. On a fork of a 45,154-item thread the newest page takes 449 to 464 ms here and 448 to 465 ms on main.

      The full-index read is the text of main again. The window has its own bounded statement. readTimelineIndex and readSnapshotTimelineIndex are not touched, so forks, jumps and odd cursors run the code of main, with the page-scoped payload scan of #35224.

      A fresh cached index wins over a window. #24422 landed after this branch was written. When a read that needs the full index has left it cached and no event has dropped it, a page uses it. Otherwise a page reads a window and builds no index.

      Hidden rows of cancelled runs come from the conversation index. The old lookup fetched every row of each cancelled run to find its queued turn: 2,587 row fetches for 31 rows on the 45,154-item thread, the largest part of a cold page read.

      The scan assertion from the #35224 review is in. ProjectionStore.test.ts now checks the run ids the payload scan binds: a run on the page is scanned, a run after the cursor is not. A variant that scans every run of the thread fails it.

      On the CodeRabbit notes: the body follows the PR template and has a Scope section. olderUserTurn and rowsBelow stay one SQL statement each inside the named ProjectionStore.selectHistoryPageInWindow operation, as answered above.

      Proof and numbers are in the PR body.

Sign in to join this conversation.