Files
pipelines/app/tool/perf/terminal_parse_bench.dart
T
alan f7ef4d1445 Terminal emulator: faster write path (ASCII runs, reused lines, no per-char allocation)
- Parser reads the written strings directly (no rune list per write) and
  handles printable ASCII runs and C0 controls straight from the string;
  a run is written into the line in one call (Buffer.writeAscii,
  BufferLine.setAsciiCells), as writeChar would one by one.
- A line leaving a full scrollback (or a one-line region scroll) is
  cleared and reused instead of allocating a 4 KiB line; lines with
  anchors (selection ends) still leave detached. Lines track the cells
  written since made or cleared, so a clear touches only those.
- Grapheme join rejects characters that cannot join before reading the
  cell (it built a String per character); width table covers plane 1.
- Switch dispatch for C0 controls, direct calls for CSI and SGR (torn-off
  handlers are slow in JavaScript); CSI params emptied without clear();
  circular buffer index without %; insert keeps the start index wrapped.
- Terminal.write(notify: false) lets a host notify once per frame.
- tool/perf/terminal_parse_bench.dart: parse throughput in the VM or V8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 02:34:08 +00:00

111 lines
3.8 KiB
Dart

// Emulator parse bench in the Dart VM: feeds generated output into
// `Terminal.write` in the app's feed slices (16 Ki units) and prints MB/s
// per workload. Pure Dart (the emulator core has no Flutter imports).
//
// dart run tool/perf/terminal_parse_bench.dart [--mb 16] [--workloads seq,yes,...]
//
// In V8, as the web build runs it:
// dart compile js -O2 -Dworkloads=seq,yes tool/perf/terminal_parse_bench.dart -o /tmp/parse.js
// node -e "globalThis.self = globalThis; require('/tmp/parse.js')"
//
// Workloads match tool/perf/terminal_stream_bench.dart.
import 'package:xterm/core.dart';
void main(List<String> args) {
var megabytes = 16.0;
var workloads = ['seq', 'yes', 'unicode', 'colors', 'screen', 'progress'];
// Compiled to JavaScript (dart compile js, run in node) there are no
// arguments: -Dworkloads=seq,yes picks them.
const only = String.fromEnvironment('workloads');
if (only.isNotEmpty) workloads = only.split(',');
for (var i = 0; i + 1 < args.length; i++) {
if (args[i] == '--mb') megabytes = double.parse(args[i + 1]);
if (args[i] == '--workloads') workloads = args[i + 1].split(',');
}
for (final workload in workloads) {
final data = generateWorkload(workload, (megabytes * 1e6).round());
// Warm up, then measure the best of three.
_run(data.substring(0, data.length ~/ 8));
var best = double.infinity;
for (var round = 0; round < 3; round++) {
final seconds = _run(data);
if (seconds < best) best = seconds;
}
final mb = data.length / 1e6;
// ignore: avoid_print
print(
'{"workload":"$workload","mb":${mb.toStringAsFixed(2)},'
'"seconds":${best.toStringAsFixed(3)},'
'"mbPerSecond":${(mb / best).toStringAsFixed(2)}}',
);
}
}
double _run(String data) {
final terminal = Terminal(maxLines: 10000);
terminal.resize(158, 48);
var notified = 0;
terminal.addListener(() => notified++);
const slice = 16 * 1024;
final clock = Stopwatch()..start();
for (var at = 0; at < data.length; at += slice) {
final end = at + slice < data.length ? at + slice : data.length;
terminal.write(data.substring(at, end));
}
clock.stop();
if (notified == 0) throw StateError('no writes');
return clock.elapsedMicroseconds / 1e6;
}
/// Generated output of about [size] UTF-16 units, as the web bench makes.
String generateWorkload(String workload, int size) {
final out = StringBuffer();
var counter = 0;
while (out.length < size) {
counter++;
switch (workload) {
case 'yes':
out.write('y\r\n');
case 'unicode':
out.write(
'$counter é ñ 中文 \u{1F600} \u{1F44D}\u{1F3FD} '
'\u{1F1F5}\u{1F1F9} │ ╭─╮ ▓▒░ '
'\u{E0B0} done\r\n',
);
case 'colors':
final c = 31 + counter % 7;
out.write(
'\x1b[${c}m$counter\x1b[0m \x1b[1mbold\x1b[0m '
'plain text line with some words\r\n',
);
case 'screen':
out.write('\x1b[H');
for (var row = 0; row < 48; row++) {
final c = 31 + (row + counter) % 7;
out.write(
'\x1b[${c}m${(counter + row).toString().padLeft(8)}'
'\x1b[0m \x1b[7m cpu ${row % 100}% \x1b[0m '
'${'process name and arguments ' * 4}\x1b[K\r\n',
);
}
case 'progress':
if (counter == 1) {
for (var row = 0; row < 47; row++) {
out.write(
'\x1b[3${row % 7 + 1}mline $row\x1b[0m '
'${'static output that does not change ' * 3}\r\n',
);
}
}
final done = counter % 50;
out.write(
'\r\x1b[1m[${'#' * done}${'.' * (50 - done)}]\x1b[0m '
'$counter files',
);
default:
out.write('$counter\r\n');
}
}
return out.toString();
}