Skip to content

Limit the timeline cache by bytes instead of row count - #7063

Merged
ymichael merged 1 commit into
mainfrom
perf/timeline-cache-byte-limit
Nov 4, 2026
Merged

ymichael merged 1 commit into
mainfrom
perf/timeline-cache-byte-limit

Conversation

@ymichael

@ymichael ymichael commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The timeline response cache skipped every window over 200 rows. Row count is a poor proxy for size: on a live database, the latest window of 72 of the 150 most recently updated threads was over 200 rows (Claude Code's finished turns stay flat, so one busy turn gives 140 command rows), while those windows were only modestly larger, median 318 KiB and at most 732 KiB, against 130 KiB for the cached ones. Switching back to any busy thread therefore always rebuilt its window. Follows #7034.

What changed

apps/server/src/services/threads/timeline-cache.ts:

  • A window is cacheable up to 2 MiB, measured with timelineWindowJsonLength, the same measure the served-windows cache uses. It reads the row digests timelineWindowTag already computed, so measuring adds no serialization.
  • The cache holds at most 64 MiB in total and evicts least-recently-used entries past it.
  • With bytes bounding memory, the entry limit rises from 128 to 512. Once busy windows are cacheable, a rotation of more than 128 threads otherwise evicts every entry just before it is reopened (measured below).

How you verified

  • Tests: a 500-row window is cached, a window over the entry byte limit is not (and drops the stale revision), the total byte budget evicts the least-recently-used entry, and byte accounting stays exact across revision replacement and invalidation. Removing the byte subtraction fails three of them.
  • Benchmark on a VACUUM INTO copy of a live database (8.4 GB): createThreadTimelineWindows().read for the latest window (deferContent, as the app requests) of the N most recently updated threads, once cold, then three reopen passes.
threads in rotation instant reopens, row limit instant reopens, this PR total reopen time
30 20 / 30 30 / 30 223 ms → 4 ms
100 54 / 100 100 / 100 1,054 ms → 17 ms
150 77–81 / 150 150 / 150 1.5–3.1 s → 27–33 ms
  • With a 128-entry limit the 150-thread rotation dropped to 2–4 instant reopens out of 150, which is why the entry limit rises.
  • Heap after a forced GC with all 150 threads cached: 191 MiB with the row limit, 217–223 MiB with this PR, about 30 MiB more, matching the measured JSON size of the windows.
  • pnpm exec turbo run typecheck lint --filter=@bb/server, and the server test/services/threads and test/public/public-thread-timeline* suites (38 files, 343 tests) pass.

AGENT GENERATED

🤖 Generated with Claude Code

The response cache skipped every window over 200 rows. On real data that
was half of recently used threads, so switching back to a busy thread
always rebuilt it, although those windows were only modestly larger (up to
about 0.7 MiB). Windows are now cacheable up to 2 MiB each, measured with
the row digests the tag already computed, and the cache holds at most
64 MiB in total. With bytes bounding memory, the entry limit rises from
128 to 512 so a rotation of more than 128 threads no longer evicts every
window just before it is reopened.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit b934641 into main Nov 4, 2026
30 checks passed
@ymichael
ymichael deleted the perf/timeline-cache-byte-limit branch November 4, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant