perf(desktop): two-phase transcript render budget for fast session switches - #363
Merged
Merged
Conversation
…itches Session switches into heavy transcripts froze the renderer for multiple seconds (profiled on a 1,294-message session: ~8s to settled paint, in 350-640ms main-thread long tasks). The switch commit rendered the full 300-part budget synchronously — every sticky user-bubble clamp measure, every Shiki tokenization, and the resulting reflow/GC churn all landed in one commit. Split the budget into two phases: the switch commit renders only the newest 80 parts (the visible tail — the thread pins to the bottom anyway), then idle-time steps of 110 parts raise it back to 300. Old groups keep their keys across steps so each raise only mounts the newly visible turns. Before each step the scroll distance-from-bottom is recorded and restored through the same mechanism 'Show earlier' uses, so the viewport never jumps. The raise never lowers a budget the user grew via 'Show earlier', and a mid-raise session switch cancels cleanly. Measured via CDP-driven switch loop on the MBP: see PR body.
Kyzcreig
enabled auto-merge (squash)
July 16, 2026 05:26
Collaborator
Author
E2E on the MBP (CDP-driven, new build
|
| Session | Before (settled paint, old build) | After (first paint, new build) |
|---|---|---|
| f48256 (1,294 msgs) | 7,984ms | 179ms |
| 6dbc21 (581 msgs) | 1,624ms | 131ms |
| c0b032 (920KB) | 3,042ms | 956ms |
| b74e98 | 2,715ms | 1,310ms |
| 798cf7 | 1,011ms | 1,063ms |
Worst main-thread block during a switch+backfill window: 503–1,175ms (the backfill steps still tokenize Shiki fences — that's the remaining lever, tracked separately). The heaviest sessions went from ~8s frozen to sub-200ms first paint with history backfilling on idle.
Methodology: Performance.now() bracket around location.hash switch, polling [data-slot=aui_turn-pair] at 100ms; longtask PerformanceObserver captures the switch+4s window. Debug port closed and app relaunched clean after measurement.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
Session switches on the MBP felt frozen — clicking a heavy session held the renderer's main thread for seconds.
Profile (CDP-driven, live app, MBP 2026-07-15)
Switch→settled-paint, before: 7,984ms on a 1,294-msg session; 1.0-3.0s on mid-size ones. V8 sampling profile attributed the time to (1) the sticky user-bubble clamp-measure ResizeObserver cascade, (2) synchronous Shiki tokenization of every visible code fence, (3) reflow/GC churn — all inside the single 300-part switch commit, in 350-640ms long tasks. Network/gateway exonerated (transcript REST fetch of the same session: 0.11s).
Fix
Two-phase render budget (
use-two-phase-render-budget.ts):requestIdleCallbackw/ 2s timeout, setTimeout fallback) raise it back to 300. Old groups keep keys → each step mounts only the new turns.Tests
7 new hook tests (slim start, stepwise raise, onBeforeRaise ordering, switch reset, show-earlier guard, setTimeout fallback, unmount cancel). Full desktop suite: 163 files / 1,317 tests green. tsc clean.
E2E
Before/after switch-latency numbers from the same CDP loop on the MBP to follow in comments.