Terminal notes: client throughput round 3 (hot spots, changes, benches)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 03:26:54 +00:00
parent a3149eef03
commit 9d46ac1c03
2 changed files with 126 additions and 4 deletions
+115 -4
View File
@@ -235,9 +235,11 @@ never waited for and keeps the old pacing (newest 64 KiB, one frame per
frames that follow them, which start exactly at the reply's `output_end`.
Client: `ChatShellTerminalFeed` (shared with chat shells) writes output into
the emulator in 16 Ki-unit slices and yields to the event loop after 6 ms,
so a 1 MiB replay or a burst never holds a UI frame; `drawn` is the server
byte end of what has been written, which `TerminalTab` acknowledges.
the emulator in slices of at most 16 Ki units and yields to the event loop
after 6 ms, so a 1 MiB replay or a burst never holds a UI frame; `drawn` is
the server byte end of what has been written, which `TerminalTab`
acknowledges. Round 3 (below) made the slices follow the measured speed,
the yields message tasks on the web, and the view's updates one per frame.
Chat shell runs (`chat_shell_supervise.go`) share the output buffer code:
output is pushed as it arrives, oldest first, while every watcher of the
@@ -259,7 +261,109 @@ old server or a client without `flow`):
Emulator (`chat_shell_terminal_feed_test.dart`, 896 KiB replay): one
synchronous write 160 ms; sliced, the same 161 ms over 20 turns, the
longest turn 12.5 ms.
longest turn 12.5 ms. After round 3 (Linux, test JIT): one write 38 ms,
sliced 33 ms over 5 turns, the longest 11-12 ms (the first, cold, call).
## Client throughput (round 3, 2026-11-26)
The emulator parsed about 1 MB/s of `seq` and 0.6 MB/s of `yes` on the web,
so the server's flow control held a flooding program to that. Hot spots
(CPU profiles of the VM and of the web build, `tool/perf/terminal_stream_profile.mjs`):
- every new line allocated a 4 KiB `Uint32List` (256-cell capacity), 65 %
of `yes` in the VM; reusing a dropped line then cleared all 4 KiB;
- each write decoded the string into a growable rune list;
- every character went through the parser's ByteConsumer, a torn-off
handler, `charset.translate`, the width table and `joinPreviousCell`,
which built a String of the previous cell (`cellContentText`) for each;
- C0 controls, ESC and CSI dispatch called torn-off closures (several times
a direct call in JavaScript); CSI reset `clear()`ed three lists (setting a
JS array's length, 20 % of a coloured stream); `%` in the circular buffer;
- on the web the feed's zero-length timers were clamped to 4 ms after a few
(a third of a burst idle), and every write notified the view, so a stream
drew a frame per turn, each rasterised on the same thread.
Changes (vendored emulator, `third_party/xterm` README lists them; feed in
`lib/features/chat/shell/`):
- The parser reads the written strings directly; runs of printable ASCII
and C0 controls are handled straight from the string
(`EscapeParser._processPlain`), a run written into the line in one call
(`Buffer.writeAscii`, `BufferLine.setAsciiCells`) exactly as `writeChar`
would (checked cell by cell in `write_path_test.dart`).
- A line leaving a full scrollback (or a one-line region scroll) is cleared
and reused; a line with anchors (selection ends) still leaves detached.
Lines track the highest cell written since made or cleared, so a clear
touches only those (`getTrimmedLength` scans only those too).
- `joinPreviousCell` rejects characters that cannot join before reading the
cell; the width table covers plane 1 (emoji) instead of searching ranges.
- Switch dispatch for C0, direct calls for CSI and SGR, CSI parameters
emptied element by element, circular index without `%`
(`IndexAwareCircularBuffer.insert` now keeps the start index wrapped).
- Feed: on the web a turn yields through a `MessageChannel` message task
(`chat_shell_terminal_yield_web.dart`), not a clamped timer. Slices are
cut to what the measured speed writes in the turn's remaining budget
(first write 4 Ki units, at least 1 Ki, at most 16 Ki), so a turn ends
near 6 ms for any output. `Terminal.write(notify: false)`: the feed shows
the state (`notifyListeners`) once everything queued is written, before
a queued action, and while still behind after a turn at most every 32 ms.
On the web (frames rasterise on the parsing thread) output written less
than 32 ms after the last show is shown when the gap has passed, about
30 frames a second while streaming; output after a quiet spell (an echo)
shows at once. Natively frames rasterise on their own thread, so written
output keeps showing at once. Lines scrolled off between two frames are
never drawn (the painter draws the visible rows of the final state).
Benches (same Linux machine, 16 cores, shared with other agents; load
average noted; before = the base commit e526150b with the same bench):
- `tool/perf/terminal_parse_bench.dart` (16 MB per workload into
`Terminal.write` in 16 Ki slices, 158x48, best of three). Dart VM
(`dart run`, JIT), after; and the same compiled with
`dart compile js -O4` (what the web build uses) in node/V8:
| workload | VM before | VM after | V8 before | V8 after |
| --- | --- | --- | --- | --- |
| `seq` | 2.2 | 31.5 | 3.4 | 37.4 |
| `yes` | 0.9 | 27.4 | 1.4 | 28.3 |
| unicode lines | 4.9 | 13.7 | 7.6 | 16.1 |
| coloured lines | 6.1 | 33.6 | 9.1 | 39.4 |
| `screen` | 10.2 | 73.0 | 15.6 | 65.1 |
| `progress` | 9.6 | 93.6 | 16.0 | 73.7 |
(MB/s; before and after run back to back at load average 19-23, the
machine busy with other agents' builds. At load 2-4 the VM reached
about 55 MB/s for `seq` and 31 for `yes` with an earlier state of these
changes.)
- `tool/perf/terminal_stream_bench.dart` (profile web build, headless
Chromium, CanvasKit software raster, 1280x800, 8 MB per workload; the
producer now follows a 256 KiB flow window past what is drawn in 32 KiB
frames, at up to 100 MB/s; "parse" is MB per second spent inside
`Terminal.write`; "MB/s" is the whole stream, frames included; two
interleaved runs each, load 2-4):
| workload | parse before | parse after | MB/s before | MB/s after | after, a frame per write |
| --- | --- | --- | --- | --- | --- |
| `seq` | 2.4 | 54-60 | 1.25 | 20.8-22.1 | 12.6-13.8 |
| `yes` | 1.2 | 38-42 | 0.66-0.69 | 18.4-18.9 | 11.5-12.9 |
| unicode lines | 7.4 | 23-25 | 2.4-2.5 | 7.6-8.0 | 6.5-7.0 |
| coloured lines | 9.7-10.0 | 57-61 | 3.6 | 25.1-25.7 | 14.0-15.1 |
| `screen` | 18.2-18.3 | 105-115 | 6.2-6.3 | 27.6-27.9 | 17.9-21.9 |
| `progress` | 18.4 | 124-135 | 6.3-9.0 | 24.8-28.6 | 20.4-22.3 |
Frame times at the same output rate (0.5 MB/s, both builds keep up):
build p50/p90 and raster p50/p90 equal or lower after (e.g. `seq` 1.4/1.9
and 8.2/8.9 ms against 1.5/2.0 and 8.2/9.1). While flooding, a stream
draws 11-16 frames for 8 MB instead of 250-470; with so few frames the
p90 is the start-up frame (max raster 48 ms in both modes). Page rAF
p50 stays 16.7 ms. The rendered final screens match the base
(`/tmp/terminal-round3/shots/`).
The end-to-end rate is now bounded by frames (each software-rasterised
frame costs 8-14 ms of the page's one thread) and by the 256 KiB flow
window (a frame of 30 ms leaves at most 256 KiB to parse after it). A
larger window on the server, or GPU raster, would raise it further.
## Screen state across reloads (2026-11-26)
@@ -507,6 +611,13 @@ software) costs about 2 ms more raster. Runs on this machine vary by about
`test/third_party_xterm/{grapheme,glyph_paint}_test.dart`. Symbols font
`fonts/symbols-nerd-font/`, `lib/ui/text/terminal_symbols_font.dart`.
Bench `tool/perf/terminal_stream_bench.{dart,mjs}`.
- Client throughput (round 3): feed `lib/features/chat/shell/chat_shell_terminal_{feed,yield,yield_stub,yield_web}.dart`;
emulator `third_party/xterm/lib/src/core/escape/parser.dart`,
`core/buffer/{buffer,line,grapheme_join}.dart`, `utils/{byte_consumer,circular_buffer}.dart`;
tests `test/third_party_xterm/write_path_test.dart`,
`test/features/chat/shell/chat_shell_terminal_feed_test.dart`; benches
`tool/perf/terminal_parse_bench.dart` (VM or V8),
`tool/perf/terminal_stream_profile.mjs` (CPU profile of the web bench).
## Verification