szn

Bug 478 — feedPty feeds the input parser byte-by-byte

478. Per-byte parser.advance on the PTY read hot path

Severity: MEDIUM (performance) Status: ✅ FIXED Files: src/window.zig:169-178

Problem

pub fn feedPty(self: *Pane) Error!void {
    const pty = &(self.pty orelse return);
    var buf: [4096]u8 = undefined;
    const n = try pty.readOutput(&buf);
    const parser = self.getParser();
    for (buf[0..n]) |byte| {
        try parser.advance(byte);
    }
    self.dirty = true;
}

Every PTY read event (up to 4096 bytes) drives the InputParser state machine one byte at a time. Each advance(byte) is a full dispatch on the parser state, followed by UTF-8 reassembly, width lookup, and writeChar. For a busy pane (cat bigfile, TUIs redrawing, compiler output) this is the single biggest per-byte cost in the server: ~4096 state-machine calls per read event, dominated by call overhead and branch mispredictions on mostly-printable-ASCII output.

This is the hottest path in the server: every byte a child program writes lands here. Typing latency and scrollback cost scale linearly with the amount of output.

Proposed Fix

Add a batch API to the parser, e.g. InputParser.advanceBatch(bytes: []const u8), that fast-forwards consecutive printable-ASCII (and known single-cell) bytes in one tight loop — skipping the state switch until it hits an ESC, C1 control, or UTF-8 lead byte — then delegates those to the existing byte path. The hot case (bulk text output) becomes a memcpy-plus-loop instead of 4096 dispatches; escape/control handling is unchanged.

Verify with a throughput test that pipes a large buffer through feedPty and asserts completion in a bounded time / measured dispatch count.

Resolution

Added InputParser.advanceBatch (src/input.zig). While the parser is in .ground with no pending UTF-8 it walks the longest run of printable ASCII (0x20–0x7E) and hands it to Screen.writeStr in one call, delegating control bytes and multibyte sequences to the existing byte-at-a-time state machine. InputParser.feed and Pane.feedPty (src/window.zig:169) now use it, so a 4096-byte read of ordinary text costs one run walk instead of 4096 advance dispatches.

Test: advanceBatch matches byte-at-a-time advance — bug #478 feeds nine mixed buffers (plain, wrapping, CSI, CR/LF/tab, UTF-8, CAN abort, OSC, invalid lead byte, DCS sixel) through both paths and compares parser state, cursor, every grid cell and wrap flag.

Verified: zig build test -Doptimize=ReleaseFast — 1012/1012 tests pass.