diff --git a/app/notes/chat-page-perf.md b/app/notes/chat-page-perf.md new file mode 100644 index 00000000..fdc9029b --- /dev/null +++ b/app/notes/chat-page-perf.md @@ -0,0 +1,76 @@ +# Chat page: per-frame work while typing, streaming and running + +Operator report: "the chat page itself is kinda slow" (web, Chromium on +Linux, real GPU). Measured with `tool/perf/chat_4k_bench.mjs` (profile build, +CDP FrameTiming plus V8 CPU profiles attributed through the source map) at +1920x1080 with the GPU, fixture +`--data 'chats=20&msgs=200,60&rich=1×tamps=1&turnactivity=1&bgwork=1' +--live 150`. The bench gained `--data` (fixture options), `--frames` (every +frame's build/raster in the JSON), a `ctrltab` scenario, and runs on a `--wasm` +build (no source map: profiles stay unattributed). + +## Causes and fixes + +- **Running composer border** (largest app cost while a turn runs): every + frame cut ~60 pieces out of the pill's `Path` with `extractPath`, which the + web engine rebuilds again on draw. `RunningRRectOutline` + (`composer_running_outline.dart`) draws the same pieces as lines and arcs of + the rounded rectangle, starting where `Path.addRRect` starts. Other shapes + keep the path. CPU in the border over a 9 s stream: 527 ms -> 163 ms. +- **The composer bar rebuilt on every keystroke and every streamed frame**: + it was a `ListenableBuilder` on the composer controller, which notifies on + each draft edit and each `chat_event` frame, and `_onDraftEdited` always + called `setState`. It now rebuilds only when what it shows changes + (sending, queued, errors, shell mode, blank draft, a `/` command draft); + the field shows the text itself. The send button's usage ring rebuilt the + button on every thread notification; it now rebuilds only when its tooltip + or arc changes. `test/features/chat/conversation/chat_composer_rebuild_test.dart`. +- **A spinner in a list row relaid out the whole list every frame**: an + element dirtied under a `LayoutBuilder` (a chat list row's `AppListRow`, a + transcript row's) marks that builder for layout, and a row in a sliver list + then relays the list and viewport out on every tick. `AppSpinner` builds + under its own `LayoutBuilder`, a relayout boundary in its fixed square; the + response-pending dots do the same in a fixed box + (`test/ui/status/app_spinner_test.dart` fails on the old code: 13 row + layouts in 10 frames, now 3). The thread's loading spinner, the list's + loading indicator and the attachment thumbnail's spinner use `AppSpinner`, + so no per-frame ancestor walk for the controller. + +## Numbers (median of 3 loads) + +JS profile build (CanvasKit), FrameTiming build ms: + +| Scenario | before p50/p90/max, sum | after p50/p90/max, sum | +| --- | --- | --- | +| type 74 chars | 4.7/6.3/14, 479 | 3.7/5.1/11.2, 395 | +| stream 150 deltas | 2.7/7.3/13.4, 2596 | 2.0/6.0/10.6, 2065 | +| scroll 120 ticks | 2.3/12.3/24.3, 1722 | 2.4/11.7/25.8, 1713 | +| switch 6 chats | 2.5/5.5/52.4, 1303 | 2.1/5.2/51.7, 1207 | + +CPU per frame part, type: build scope 155 -> 77 ms, layout 228 -> 148 ms. +Stream: paint 995 -> 589 ms; the chat list's per-frame relayout (98 ms) and +the composer's per-delta rebuild (84 ms) are gone. + +Wasm profile build (skwasm, isolation headers), FrameTiming build ms: + +| Scenario | before p50/p90/max, sum | after p50/p90/max, sum | +| --- | --- | --- | +| type | 2.1/2.4/3.7, 200 | 1.7/2.1/4.7, 172 | +| stream | 1.1/3.3/8.5, 1102 | 0.9/3.1/4.0, 953 | +| scroll | 1.2/6.9/16.1, 816 | 1.2/6.5/13.9, 797 | +| switch | 1.0/2.4/26.7, 598 | 1.0/2.5/27.0, 577 | +| open | 1.0/4.3/38.4, 188 | 0.9/3.2/34.1, 181 | + +Raster p90 stays under 3 ms with the GPU in every scenario. + +## Left open (measured, not changed) + +- Opening or switching a chat costs one ~40-50 ms frame (the new pane's + header, composer and loading row) and one ~35 ms frame when the history + arrives (rows on screen, markdown, table measuring). Framework build and + layout of those widgets, not app logic; rows off screen are not built. +- Scrolling fast: the cost is mounting and laying out newly visible rows; + table layout is about 6% of it (`ChatTableCellMeasure` caches only repeat + views). Selection ordering no longer shows. +- Per delta the transcript rows are re-derived (about 0.7 ms per delta: + activity groups, day dividers, running compaction scan). diff --git a/app/tool/perf/chat_4k_bench.mjs b/app/tool/perf/chat_4k_bench.mjs index 95f18686..0df58ea7 100644 --- a/app/tool/perf/chat_4k_bench.mjs +++ b/app/tool/perf/chat_4k_bench.mjs @@ -88,7 +88,8 @@ function url(origin, extra = {}) { } const server = await serveBuild(opts.build); -const lookup = await loadSourceMap(opts.build); +// A `--wasm` build has no JS source map: CPU profiles then stay unattributed. +const lookup = await loadSourceMap(opts.build).catch(() => () => null); await mkdir(opts.out, { recursive: true }); // ---- Discovery: where the rows, the composer and the transcript are --------