Terminal feed: on the web, show streaming output at most every 32 ms

In the browser a frame rasterises on the thread that parses output, so a
stream drawn at 60 frames a second spends half its time drawing. With
deferShows (the web default) output written less than a gap after the
last show is shown when the gap has passed; output after a quiet spell
(an echo) still shows at once. Natively frames rasterise on their own
thread and written output keeps showing at once. The stream bench
measures the stream without its final 600 ms wait for frame timings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 03:14:17 +00:00
parent ae07fe14db
commit a3149eef03
6 changed files with 89 additions and 19 deletions
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:collection';
import 'chat_shell_terminal_yield.dart';
@@ -9,9 +10,10 @@ const int chatShellFeedSlice = 16 * 1024;
/// large replay or burst never holds a UI frame for long.
const Duration chatShellFeedBudget = Duration(milliseconds: 6);
/// While output waits after a turn, the view is shown the emulator's state
/// at most this often (about 30 frames a second), so a flood spends its
/// time parsing rather than drawing screens nobody can read.
/// While output streams, the view is shown the emulator's state at most
/// this often (about 30 frames a second), so a flood spends its time
/// parsing rather than drawing screens nobody can read. Output after a
/// quiet spell (an echo, a prompt) is shown at once.
const Duration chatShellFeedShowGap = Duration(milliseconds: 32);
/// The fewest units a write takes while more waits, however slow the
@@ -31,9 +33,13 @@ const int _firstSlice = 4096;
/// consumed).
///
/// With [show], writes do not notify the view themselves: [show] is called
/// once everything queued is written, and while output still waits after
/// a turn at most every [showGap], so a burst draws its final state per
/// frame instead of one frame per turn.
/// once everything queued is written, before an action queued with [then],
/// and while output still waits after a turn at most every [showGap]. A
/// burst draws its final state per frame instead of one frame per turn.
/// With [deferShows] (the web, where frames rasterise on the parsing
/// thread) written output is also shown at most every [showGap]: output
/// written less than a gap after the last show is shown when the gap has
/// passed, so a stream draws about 30 frames a second, not one per piece.
class ChatShellTerminalFeed {
ChatShellTerminalFeed(
this._write, {
@@ -41,6 +47,7 @@ class ChatShellTerminalFeed {
this.budget = chatShellFeedBudget,
this.slice = chatShellFeedSlice,
this.showGap = chatShellFeedShowGap,
this.deferShows = chatShellRasterSharesThread,
});
/// Writes [text] into the emulator; [replay] marks a redraw of old output.
@@ -51,10 +58,13 @@ class ChatShellTerminalFeed {
final Duration budget;
final int slice;
final Duration showGap;
final bool deferShows;
/// Since [show] was last called; whether anything was written since.
final Stopwatch _sinceShown = Stopwatch()..start();
/// Since [show] was last called (not running before the first call);
/// whether anything was written since; the show waiting for the gap.
final Stopwatch _sinceShown = Stopwatch();
bool _unshown = false;
Timer? _showLater;
/// Called with a piece's end once it is drawn.
void Function(int end)? onDrawn;
@@ -98,6 +108,7 @@ class ChatShellTerminalFeed {
/// Runs [action] once everything queued before it is written.
void then(void Function() action) {
if (_pieces.isEmpty && !_pumping) {
_show();
action();
return;
}
@@ -108,6 +119,8 @@ class ChatShellTerminalFeed {
_pieces.clear();
_cancel?.call();
_cancel = null;
_showLater?.cancel();
_showLater = null;
}
void _schedule() {
@@ -160,21 +173,32 @@ class ChatShellTerminalFeed {
}
}
if (_pieces.isNotEmpty && clock.elapsed >= budget) {
if (_sinceShown.elapsed >= showGap) _show();
if (_gapPassed) _show();
_cancel = chatShellYield(_pump);
return;
}
}
_show();
if (!deferShows || _gapPassed) {
_show();
} else if (_unshown) {
_showLater ??= Timer(showGap - _sinceShown.elapsed, _show);
}
} finally {
_pumping = false;
}
}
bool get _gapPassed =>
!_sinceShown.isRunning || _sinceShown.elapsed >= showGap;
void _show() {
_showLater?.cancel();
_showLater = null;
if (!_unshown) return;
_unshown = false;
_sinceShown.reset();
_sinceShown
..reset()
..start();
show?.call();
}
}
@@ -9,3 +9,7 @@ import 'chat_shell_terminal_yield_stub.dart'
/// a third of a large burst. Elsewhere it is a zero-length timer.
void Function() chatShellYield(void Function() callback) =>
impl.chatShellYield(callback);
/// Whether frames rasterise on the thread that parses output (the web),
/// so that drawing fewer of them while output streams lets it parse more.
const bool chatShellRasterSharesThread = impl.chatShellRasterSharesThread;
@@ -2,3 +2,7 @@ import 'dart:async';
void Function() chatShellYield(void Function() callback) =>
Timer(Duration.zero, callback).cancel;
/// Natively a frame rasterises on its own thread; the UI thread only
/// builds it, so frames take little from parsing.
const bool chatShellRasterSharesThread = false;
@@ -4,6 +4,10 @@ import 'dart:js_interop';
import 'package:web/web.dart' as web;
/// In the browser a frame rasterises on the page's one thread, between the
/// emulator's turns: every frame drawn is time not spent parsing.
const bool chatShellRasterSharesThread = true;
/// One channel for every terminal: each message runs the oldest waiting
/// callback, in order.
final _waiting = Queue<_Turn>();