diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 17:01:35 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 19:12:05 -0300 |
| commit | 97bb0e816513b805ca13358d8a14ba117f586403 (patch) | |
| tree | 209405ca605f6b49d1a15adc486ff9dfc696a1fb /src/tty/tty.zig | |
| parent | efcb4c35e76ef33bbd62123a5c5574f88bc8bcc7 (diff) | |
| download | pardes-97bb0e816513b805ca13358d8a14ba117f586403.tar.gz pardes-97bb0e816513b805ca13358d8a14ba117f586403.zip | |
tty: a flooded pane's output is drawn a frame a tick at most, two of its reads queued at most, so it costs no frame a 4 KB read and holds no key or echo behind it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/tty/tty.zig')
| -rw-r--r-- | src/tty/tty.zig | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/src/tty/tty.zig b/src/tty/tty.zig index d474e83a..760c0692 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1407,7 +1407,10 @@ fn localSession( sh.inotify_fd = -1; } while (loop.tryEvent() catch null) |ev| switch (ev) { - .pty_read => |pr| gpa.free(pr.bytes), + .pty_read => |pr| { + tookRead(io, pr.id); + gpa.free(pr.bytes); + }, .paste => |b| gpa.free(@constCast(b)), .lsp_done => |d| if (d.rows) |rows| allocs.lsp.free(rows), .lsp_status => |text| allocs.lsp.free(text), @@ -1591,6 +1594,13 @@ const Shell = struct { pace_ns: u64 = 0, input_seen: bool = false, wake_to: u64 = 0, + /// waitInput's pacing of output: whether this wait read a pane's output, + /// and when the last frame was written, the last input was read and the + /// focused pane's output last was. + output_seen: bool = false, + drawn_ns: u64 = 0, + input_ns: u64 = 0, + focused_ns: u64 = 0, fn of(ctx: ?*anyopaque) *Shell { return @ptrCast(@alignCast(ctx.?)); @@ -1778,6 +1788,7 @@ const Shell = struct { wait = @intCast(@max(1, (tick -| now) / std.time.ns_per_ms)); } s.input_seen = false; + s.output_seen = false; // Paced, what is not an input (a frame's ack, a pty's output) is // taken in and the wait goes on to the tick: the acks alone ran the // loop at the terminal's round trip. @@ -1789,6 +1800,21 @@ const Shell = struct { wait = @intCast((tick - now) / std.time.ns_per_ms); } if (paced and s.input_seen) s.pace_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + // Output alone is drawn a frame a tick at most, what came in till + // then in one: a flood (`yes`, 4 KB a read) was drawn a read at a + // time, nearly all of its frames written over before they were + // seen. Never while what someone waits on is undrawn: an input + // (its frame held for the terminal's answer, FrameAck), or the + // focused pane's output soon after one, its echo (a remote shell's + // an RTT later). + while (!paced and s.output_seen and !s.input_seen and !s.core.quit and s.test_clock == null) { + if (s.input_ns > s.drawn_ns) break; + if (s.focused_ns > s.drawn_ns and s.focused_ns -| s.input_ns < std.time.ns_per_s) break; + const due = s.drawn_ns + pardes.animation.frame_ns; + const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); + if (now + std.time.ns_per_ms > due) break; + s.waitOnce(@intCast((due - now) / std.time.ns_per_ms)); + } } fn waitOnce(s: *Shell, wait: u32) void { @@ -1853,6 +1879,7 @@ const Shell = struct { .paste, .paste_start, .winsize => s.input_seen = true, else => {}, } + if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); switch (event) { .nop => {}, .wake => if (s.test_clock) |*virtual| { @@ -1886,6 +1913,9 @@ const Shell = struct { core.update(.{ .resize = .{ .cols = ws.cols, .rows = ws.rows } }); }, .pty_read => |pr| { + tookRead(s.io, pr.id); + s.output_seen = true; + if (pr.id == core.active) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); if (s.gens[pr.id] == pr.gen) core.update(.{ .output = .{ .pane = @intCast(pr.id), .bytes = pr.bytes } }); s.gpa.free(pr.bytes); @@ -2139,6 +2169,7 @@ const Shell = struct { // vaxis's diff of the cells wrote. s.core.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), s.tty.tty_writer.pos + s.tty.writer().end -| before)); if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds)); + s.drawn_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); tz_render.end(); } @@ -2572,15 +2603,37 @@ fn wakeFs(ctx: ?*anyopaque) void { _ = loop.tryPostEvent(.fs_ready) catch {}; } +/// Each pane's reads posted and not yet taken in. Two at most: a flood the +/// loop cannot keep up with waits in its pty, not in the queue ahead of a +/// key and its echo (hundreds of 4 KB reads queued were half a second). +var reads_queued: [pardes.MAX_PANES]std.atomic.Value(u32) = @splat(.init(0)); + +/// A read of pane `id` taken in (or dropped): its reader may read on. Never +/// below zero, for an event no reader posted (a test's). +fn tookRead(io: std.Io, id: usize) void { + var queued = reads_queued[id].load(.acquire); + while (queued > 0) queued = reads_queued[id].cmpxchgWeak(queued, queued - 1, .acq_rel, .acquire) orelse { + if (queued == 2) io.futexWake(u32, &reads_queued[id].raw, 1); + return; + }; +} + fn readPty(io: std.Io, gpa: std.mem.Allocator, pty: std.Io.File, id: usize, gen: u32, loop: *Loop) anyerror!void { // One buffer, read into directly: a Reader around it only added a second // (and in a safe build, filled both as `undefined` on every start). var buf: [0x10000]u8 = undefined; - while (true) { + read: while (true) { + while (true) { + const queued = reads_queued[id].load(.acquire); + if (queued < 2) break; + io.futexWait(u32, &reads_queued[id].raw, queued) catch break :read; + } const n = pty.readStreaming(io, &.{&buf}) catch break; if (n == 0) break; const bytes = try gpa.dupe(u8, buf[0..n]); + _ = reads_queued[id].fetchAdd(1, .acq_rel); loop.postEvent(.{ .pty_read = .{ .id = id, .gen = gen, .bytes = bytes } }) catch { + tookRead(io, id); gpa.free(bytes); break; }; |
