Terminal feed: message-task yields, slices sized to speed, one show per frame

- On the web a turn yields through a MessageChannel task: a chain of
  zero-length timers is clamped to 4 ms each, which left the emulator
  idle for a third of a burst.
- Slices are cut to what the measured speed writes in the budget left,
  so a turn ends near 6 ms however costly the output is to parse; the
  first write takes 4 Ki units to measure.
- Writes no longer notify the view each: the feed shows the emulator's
  state once everything queued is written, and while output still waits
  after a turn at most every 32 ms, so a flood draws its final state per
  frame instead of a frame per turn.
- Stream bench: producer follows a flow window (256 KiB past drawn, 32 KiB
  frames, rate by elapsed time), reports time spent parsing, ?show=each
  for the old per-write notify; tool/perf/terminal_stream_profile.mjs
  prints a CPU profile of one workload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 02:52:41 +00:00
parent f7ef4d1445
commit ae07fe14db
8 changed files with 328 additions and 21 deletions
@@ -55,6 +55,84 @@ void main() {
},
);
test('the view is shown once what waits is written, and while a burst '
'is behind at most every show gap', () async {
var shown = 0;
final writes = <String>[];
final feed = ChatShellTerminalFeed(
(text, _) => writes.add(text),
show: () => shown++,
budget: Duration.zero,
slice: 10,
showGap: const Duration(hours: 1),
);
feed.add('abc', 3);
// Written whole at once: shown at once.
expect(writes, ['abc']);
expect(shown, 1);
feed.add('x' * 35, 38);
await turn();
await turn();
// Behind, within the gap: not shown again.
expect(writes.length, 4);
expect(shown, 1);
await turn();
await turn();
// Caught up: the final state is shown.
expect(writes.skip(1).join(), 'x' * 35);
expect(feed.idle, isTrue);
expect(shown, 2);
// An action waiting behind output sees it shown first.
feed.add('y' * 25, 63);
var shownForAction = -1;
feed.then(() => shownForAction = shown);
for (var i = 0; i < 5; i++) {
await turn();
}
expect(shownForAction, 3);
expect(shown, 3);
});
test('every turn shows its state with no show gap', () async {
var shown = 0;
final feed = ChatShellTerminalFeed(
(_, _) {},
show: () => shown++,
budget: Duration.zero,
slice: 10,
showGap: Duration.zero,
);
feed.add('x' * 30, 30);
for (var i = 0; i < 5; i++) {
await turn();
}
expect(feed.idle, isTrue);
expect(shown, 3);
});
test('slices follow the measured speed within the slice', () async {
final sizes = <int>[];
final feed = ChatShellTerminalFeed(
(text, _) {
sizes.add(text.length);
// About 1 ms per 1000 units.
final until = Stopwatch()..start();
while (until.elapsedMicroseconds < text.length) {}
},
budget: const Duration(milliseconds: 6),
slice: 16 * 1024,
);
feed.add('y' * 100000, 100000);
while (!feed.idle) {
await turn();
}
expect(sizes.reduce((a, b) => a + b), 100000);
expect(sizes.every((s) => s <= 16 * 1024), isTrue);
// After the first slice measured the speed, no write takes much more
// than a turn's budget (6 ms, about 6000 units here).
expect(sizes.skip(1).every((s) => s <= 8000), isTrue, reason: '$sizes');
});
test('a surrogate pair is never split between two writes', () {
final writes = <String>[];
final feed = ChatShellTerminalFeed((text, _) => writes.add(text), slice: 3);