# Chat transcript open performance Performance round 2 on opening a long chat. Started from measurements; no intended behaviour change apart from the differences listed below. Follows `notes/chat-stream-perf.md`. ## Finding The transcript was a top-anchored `ListView`, and the view jumped to its end after the first layout. A list can reach its end only by laying out every row in between, so opening a 300-message chat built 301 message rows and parsed 150 markdown answers in one frame before the newest message showed. ## Change - `ChatTranscriptScrollView` (`lib/features/chat/conversation/`) is a `CustomScrollView` with a centre. Rows up to an anchor row sit in a sliver that grows upwards from the centre. Later rows, plus queued, unsent and command lines, sit in a sliver that grows downwards. With `anchor: 1`, offset 0 puts the centre at the viewport's bottom, so opening lays out only the rows on screen. - `chatTranscriptAnchor` (`chat_transcript_anchor.dart`) picks the message row before the newest message row, and before any reply that is still streaming. The anchor is chosen once, when the history arrives, and stays fixed while the chat is open. Growth on either side therefore never moves the other side: older pages are prepended above, and replies stream below. - A transcript whose older side fits in the viewport (where `minScrollExtent` is 0 after the first layout) switches back to top-anchored, exactly as before. Short chats still start at the top with no gap above them. - Saved positions are now `({offset, anchor})`. An offset is restored only against the same anchor message id. If that row is gone, the chat opens at its newest message. While a restore settles, the code now also waits for `minScrollExtent` to settle. - Follow, the jump control, the arrival count, keyboard and composer insets, and per-message selection use the same logic as before. The end of the transcript is still `maxScrollExtent`. ## Numbers Widget harness `test/features/chat/perf/chat_stream_rebuild_perf_test.dart`: debug VM, 1000x1600 viewport, two runs each. "Open" covers the frames from the first one that builds a message row to the one where the newest message shows. | | before | after | | --- | --- | --- | | open 300: rows built / markdown parses | 301 / 150 | 4 / 2 | | open 300: frames / frame time sum / longest frame | 30 / 1212–1301 ms / 1090–1184 ms | 2 / 99–113 ms / 95–109 ms | | open 1000: rows built / markdown parses | 1001 / 500 | 4 / 2 | | open 1000: frames / frame time sum / longest frame | 38 / 2508–2616 ms / 2465–2566 ms | 2 / 47–54 ms / 44–51 ms | | stream 200 deltas (300 msgs): message builds | 200 | 200 | | stream: frame mean (debug) | 5.5–5.9 ms | 7.2–7.8 ms (build counts by widget type unchanged; not seen in release) | | scroll 80 drags: parses | 0 | 22 (rows not built at open are parsed the first time they show) | | scroll: frame mean | 5.5–6.0 ms | 5.4–6.4 ms | Release web probe `node tool/chat_perf_probe.mjs 200`, built from `tool/chat_composer_probe_entry.dart`. Headless Chrome rasterises in software. Before and after runs alternated. The new open phase counts the frames that do work after the history reply (`PERF_HISTORY`). | | before | after | | --- | --- | --- | | open 300: build sum (longest frame) | 608–1314 ms (590–1289) | 12.5–24 ms (5.8–11.9) | | open 300: total frame span sum | 1012–1878 ms | 218–490 ms | | open 1000: build sum (longest frame) | 1585–2945 ms (1565–2925) | 6.3–13.8 ms (4.8–8.5) | | open 1000: total frame span sum | 2101–3792 ms | 99–289 ms | | streaming build mean, 300 / 1000 | 6.1–7.2 / 11.3–16.5 ms | 5.9–12.0 / 11.5–19.5 ms (noise) | | scrolling build mean, 300 / 1000 | 1.6–2.5 / 1.7–2.6 ms | 2.3–5.1 / 2.0–3.3 ms | For 300 messages, two before runs are used (the first overlapped another probe). The fixture's `_PlatformViewPlaceholderBox` null-check error, logged by the old layout on web, no longer appears. Browser captures of the 300-message open, the scroll back, the short chat and the 12-message chat look the same before and after. ## Behaviour differences - **Load older** keeps the rows the operator was reading in place, and the new page appears above them. Before, the view stayed at the top, so the operator landed on the oldest rows of the new page. - When the viewport shrinks while the operator reads above the end (keyboard, resize), the bottom of the view stays put instead of the top. - A transcript replaced by a repair that no longer contains the anchor row is split again. A view that is not following can then move.