diff --git a/app/lib/features/chat/shell/chat_shell_terminal_feed.dart b/app/lib/features/chat/shell/chat_shell_terminal_feed.dart index 3828dce4..11689f22 100644 --- a/app/lib/features/chat/shell/chat_shell_terminal_feed.dart +++ b/app/lib/features/chat/shell/chat_shell_terminal_feed.dart @@ -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(); } } diff --git a/app/lib/features/chat/shell/chat_shell_terminal_yield.dart b/app/lib/features/chat/shell/chat_shell_terminal_yield.dart index 79ffcb26..1676ef33 100644 --- a/app/lib/features/chat/shell/chat_shell_terminal_yield.dart +++ b/app/lib/features/chat/shell/chat_shell_terminal_yield.dart @@ -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; diff --git a/app/lib/features/chat/shell/chat_shell_terminal_yield_stub.dart b/app/lib/features/chat/shell/chat_shell_terminal_yield_stub.dart index b034d8c0..0354fe0c 100644 --- a/app/lib/features/chat/shell/chat_shell_terminal_yield_stub.dart +++ b/app/lib/features/chat/shell/chat_shell_terminal_yield_stub.dart @@ -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; diff --git a/app/lib/features/chat/shell/chat_shell_terminal_yield_web.dart b/app/lib/features/chat/shell/chat_shell_terminal_yield_web.dart index acc17871..e2217236 100644 --- a/app/lib/features/chat/shell/chat_shell_terminal_yield_web.dart +++ b/app/lib/features/chat/shell/chat_shell_terminal_yield_web.dart @@ -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>(); diff --git a/app/test/features/chat/shell/chat_shell_terminal_feed_test.dart b/app/test/features/chat/shell/chat_shell_terminal_feed_test.dart index 06bdc2d3..55ec0a96 100644 --- a/app/test/features/chat/shell/chat_shell_terminal_feed_test.dart +++ b/app/test/features/chat/shell/chat_shell_terminal_feed_test.dart @@ -55,8 +55,8 @@ void main() { }, ); - test('the view is shown once what waits is written, and while a burst ' - 'is behind at most every show gap', () async { + test('output after a quiet spell is shown at once; streaming output at ' + 'most every show gap', () async { var shown = 0; final writes = []; final feed = ChatShellTerminalFeed( @@ -64,10 +64,11 @@ void main() { show: () => shown++, budget: Duration.zero, slice: 10, - showGap: const Duration(hours: 1), + showGap: const Duration(milliseconds: 300), + deferShows: true, ); 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(shown, 1); feed.add('x' * 35, 38); @@ -78,18 +79,50 @@ void main() { expect(shown, 1); 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(feed.idle, isTrue); + expect(shown, 1); + await Future.delayed(const Duration(milliseconds: 400)); expect(shown, 2); + // After a quiet spell: at once again. + await Future.delayed(const Duration(milliseconds: 400)); + feed.add('q', 39); + expect(shown, 3); // An action waiting behind output sees it shown first. - feed.add('y' * 25, 63); + feed.add('y' * 25, 64); var shownForAction = -1; feed.then(() => shownForAction = shown); for (var i = 0; i < 5; i++) { 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); }); diff --git a/app/tool/perf/terminal_stream_bench.dart b/app/tool/perf/terminal_stream_bench.dart index 59947c66..96c17ad5 100644 --- a/app/tool/perf/terminal_stream_bench.dart +++ b/app/tool/perf/terminal_stream_bench.dart @@ -178,11 +178,12 @@ class _BenchState extends State<_Bench> { void _finish() { if (_done) return; _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. SchedulerBinding.instance.scheduleFrame(); Timer(const Duration(milliseconds: 600), () { _clock.stop(); - final seconds = _clock.elapsedMicroseconds / 1e6; List ms(Duration Function(FrameTiming t) pick) => [ for (final t in _timings) pick(t).inMicroseconds / 1000, ];