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:
@@ -1,3 +1,4 @@
|
|||||||
|
import 'dart:async';
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'chat_shell_terminal_yield.dart';
|
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.
|
/// large replay or burst never holds a UI frame for long.
|
||||||
const Duration chatShellFeedBudget = Duration(milliseconds: 6);
|
const Duration chatShellFeedBudget = Duration(milliseconds: 6);
|
||||||
|
|
||||||
/// While output waits after a turn, the view is shown the emulator's state
|
/// While output streams, the view is shown the emulator's state at most
|
||||||
/// at most this often (about 30 frames a second), so a flood spends its
|
/// this often (about 30 frames a second), so a flood spends its time
|
||||||
/// time parsing rather than drawing screens nobody can read.
|
/// 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);
|
const Duration chatShellFeedShowGap = Duration(milliseconds: 32);
|
||||||
|
|
||||||
/// The fewest units a write takes while more waits, however slow the
|
/// The fewest units a write takes while more waits, however slow the
|
||||||
@@ -31,9 +33,13 @@ const int _firstSlice = 4096;
|
|||||||
/// consumed).
|
/// consumed).
|
||||||
///
|
///
|
||||||
/// With [show], writes do not notify the view themselves: [show] is called
|
/// With [show], writes do not notify the view themselves: [show] is called
|
||||||
/// once everything queued is written, and while output still waits after
|
/// once everything queued is written, before an action queued with [then],
|
||||||
/// a turn at most every [showGap], so a burst draws its final state per
|
/// and while output still waits after a turn at most every [showGap]. A
|
||||||
/// frame instead of one frame per turn.
|
/// 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 {
|
class ChatShellTerminalFeed {
|
||||||
ChatShellTerminalFeed(
|
ChatShellTerminalFeed(
|
||||||
this._write, {
|
this._write, {
|
||||||
@@ -41,6 +47,7 @@ class ChatShellTerminalFeed {
|
|||||||
this.budget = chatShellFeedBudget,
|
this.budget = chatShellFeedBudget,
|
||||||
this.slice = chatShellFeedSlice,
|
this.slice = chatShellFeedSlice,
|
||||||
this.showGap = chatShellFeedShowGap,
|
this.showGap = chatShellFeedShowGap,
|
||||||
|
this.deferShows = chatShellRasterSharesThread,
|
||||||
});
|
});
|
||||||
|
|
||||||
/// Writes [text] into the emulator; [replay] marks a redraw of old output.
|
/// Writes [text] into the emulator; [replay] marks a redraw of old output.
|
||||||
@@ -51,10 +58,13 @@ class ChatShellTerminalFeed {
|
|||||||
final Duration budget;
|
final Duration budget;
|
||||||
final int slice;
|
final int slice;
|
||||||
final Duration showGap;
|
final Duration showGap;
|
||||||
|
final bool deferShows;
|
||||||
|
|
||||||
/// Since [show] was last called; whether anything was written since.
|
/// Since [show] was last called (not running before the first call);
|
||||||
final Stopwatch _sinceShown = Stopwatch()..start();
|
/// whether anything was written since; the show waiting for the gap.
|
||||||
|
final Stopwatch _sinceShown = Stopwatch();
|
||||||
bool _unshown = false;
|
bool _unshown = false;
|
||||||
|
Timer? _showLater;
|
||||||
|
|
||||||
/// Called with a piece's end once it is drawn.
|
/// Called with a piece's end once it is drawn.
|
||||||
void Function(int end)? onDrawn;
|
void Function(int end)? onDrawn;
|
||||||
@@ -98,6 +108,7 @@ class ChatShellTerminalFeed {
|
|||||||
/// Runs [action] once everything queued before it is written.
|
/// Runs [action] once everything queued before it is written.
|
||||||
void then(void Function() action) {
|
void then(void Function() action) {
|
||||||
if (_pieces.isEmpty && !_pumping) {
|
if (_pieces.isEmpty && !_pumping) {
|
||||||
|
_show();
|
||||||
action();
|
action();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -108,6 +119,8 @@ class ChatShellTerminalFeed {
|
|||||||
_pieces.clear();
|
_pieces.clear();
|
||||||
_cancel?.call();
|
_cancel?.call();
|
||||||
_cancel = null;
|
_cancel = null;
|
||||||
|
_showLater?.cancel();
|
||||||
|
_showLater = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _schedule() {
|
void _schedule() {
|
||||||
@@ -160,21 +173,32 @@ class ChatShellTerminalFeed {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (_pieces.isNotEmpty && clock.elapsed >= budget) {
|
if (_pieces.isNotEmpty && clock.elapsed >= budget) {
|
||||||
if (_sinceShown.elapsed >= showGap) _show();
|
if (_gapPassed) _show();
|
||||||
_cancel = chatShellYield(_pump);
|
_cancel = chatShellYield(_pump);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_show();
|
if (!deferShows || _gapPassed) {
|
||||||
|
_show();
|
||||||
|
} else if (_unshown) {
|
||||||
|
_showLater ??= Timer(showGap - _sinceShown.elapsed, _show);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
_pumping = false;
|
_pumping = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool get _gapPassed =>
|
||||||
|
!_sinceShown.isRunning || _sinceShown.elapsed >= showGap;
|
||||||
|
|
||||||
void _show() {
|
void _show() {
|
||||||
|
_showLater?.cancel();
|
||||||
|
_showLater = null;
|
||||||
if (!_unshown) return;
|
if (!_unshown) return;
|
||||||
_unshown = false;
|
_unshown = false;
|
||||||
_sinceShown.reset();
|
_sinceShown
|
||||||
|
..reset()
|
||||||
|
..start();
|
||||||
show?.call();
|
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.
|
/// a third of a large burst. Elsewhere it is a zero-length timer.
|
||||||
void Function() chatShellYield(void Function() callback) =>
|
void Function() chatShellYield(void Function() callback) =>
|
||||||
impl.chatShellYield(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) =>
|
void Function() chatShellYield(void Function() callback) =>
|
||||||
Timer(Duration.zero, callback).cancel;
|
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;
|
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
|
/// One channel for every terminal: each message runs the oldest waiting
|
||||||
/// callback, in order.
|
/// callback, in order.
|
||||||
final _waiting = Queue<_Turn>();
|
final _waiting = Queue<_Turn>();
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ void main() {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
test('the view is shown once what waits is written, and while a burst '
|
test('output after a quiet spell is shown at once; streaming output at '
|
||||||
'is behind at most every show gap', () async {
|
'most every show gap', () async {
|
||||||
var shown = 0;
|
var shown = 0;
|
||||||
final writes = <String>[];
|
final writes = <String>[];
|
||||||
final feed = ChatShellTerminalFeed(
|
final feed = ChatShellTerminalFeed(
|
||||||
@@ -64,10 +64,11 @@ void main() {
|
|||||||
show: () => shown++,
|
show: () => shown++,
|
||||||
budget: Duration.zero,
|
budget: Duration.zero,
|
||||||
slice: 10,
|
slice: 10,
|
||||||
showGap: const Duration(hours: 1),
|
showGap: const Duration(milliseconds: 300),
|
||||||
|
deferShows: true,
|
||||||
);
|
);
|
||||||
feed.add('abc', 3);
|
feed.add('abc', 3);
|
||||||
// Written whole at once: shown at once.
|
// The first output, written whole at once: shown at once.
|
||||||
expect(writes, ['abc']);
|
expect(writes, ['abc']);
|
||||||
expect(shown, 1);
|
expect(shown, 1);
|
||||||
feed.add('x' * 35, 38);
|
feed.add('x' * 35, 38);
|
||||||
@@ -78,18 +79,50 @@ void main() {
|
|||||||
expect(shown, 1);
|
expect(shown, 1);
|
||||||
await turn();
|
await turn();
|
||||||
await turn();
|
await turn();
|
||||||
// Caught up: the final state is shown.
|
// Caught up within the gap: shown when the gap has passed.
|
||||||
expect(writes.skip(1).join(), 'x' * 35);
|
expect(writes.skip(1).join(), 'x' * 35);
|
||||||
expect(feed.idle, isTrue);
|
expect(feed.idle, isTrue);
|
||||||
|
expect(shown, 1);
|
||||||
|
await Future<void>.delayed(const Duration(milliseconds: 400));
|
||||||
expect(shown, 2);
|
expect(shown, 2);
|
||||||
|
// After a quiet spell: at once again.
|
||||||
|
await Future<void>.delayed(const Duration(milliseconds: 400));
|
||||||
|
feed.add('q', 39);
|
||||||
|
expect(shown, 3);
|
||||||
// An action waiting behind output sees it shown first.
|
// An action waiting behind output sees it shown first.
|
||||||
feed.add('y' * 25, 63);
|
feed.add('y' * 25, 64);
|
||||||
var shownForAction = -1;
|
var shownForAction = -1;
|
||||||
feed.then(() => shownForAction = shown);
|
feed.then(() => shownForAction = shown);
|
||||||
for (var i = 0; i < 5; i++) {
|
for (var i = 0; i < 5; i++) {
|
||||||
await turn();
|
await turn();
|
||||||
}
|
}
|
||||||
expect(shownForAction, 3);
|
expect(shownForAction, 4);
|
||||||
|
expect(shown, 4);
|
||||||
|
feed.dispose();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('without deferred shows (natively) written output is shown at once; '
|
||||||
|
'only a burst still behind waits for the gap', () async {
|
||||||
|
var shown = 0;
|
||||||
|
final feed = ChatShellTerminalFeed(
|
||||||
|
(_, _) {},
|
||||||
|
show: () => shown++,
|
||||||
|
budget: Duration.zero,
|
||||||
|
slice: 10,
|
||||||
|
showGap: const Duration(hours: 1),
|
||||||
|
deferShows: false,
|
||||||
|
);
|
||||||
|
feed.add('abc', 3);
|
||||||
|
feed.add('def', 6);
|
||||||
|
expect(shown, 2);
|
||||||
|
feed.add('x' * 35, 41);
|
||||||
|
await turn();
|
||||||
|
await turn();
|
||||||
|
expect(shown, 2);
|
||||||
|
for (var i = 0; i < 3; i++) {
|
||||||
|
await turn();
|
||||||
|
}
|
||||||
|
expect(feed.idle, isTrue);
|
||||||
expect(shown, 3);
|
expect(shown, 3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -178,11 +178,12 @@ class _BenchState extends State<_Bench> {
|
|||||||
void _finish() {
|
void _finish() {
|
||||||
if (_done) return;
|
if (_done) return;
|
||||||
_done = true;
|
_done = true;
|
||||||
|
// The stream took this long; the wait below only collects timings.
|
||||||
|
final seconds = _clock.elapsedMicroseconds / 1e6;
|
||||||
// A last frame flushes the engine's batched timings.
|
// A last frame flushes the engine's batched timings.
|
||||||
SchedulerBinding.instance.scheduleFrame();
|
SchedulerBinding.instance.scheduleFrame();
|
||||||
Timer(const Duration(milliseconds: 600), () {
|
Timer(const Duration(milliseconds: 600), () {
|
||||||
_clock.stop();
|
_clock.stop();
|
||||||
final seconds = _clock.elapsedMicroseconds / 1e6;
|
|
||||||
List<double> ms(Duration Function(FrameTiming t) pick) => [
|
List<double> ms(Duration Function(FrameTiming t) pick) => [
|
||||||
for (final t in _timings) pick(t).inMicroseconds / 1000,
|
for (final t in _timings) pick(t).inMicroseconds / 1000,
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user