From 2f2fd5f6cf52800f931fd62e539c2b4ed5d80ebc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 00:10:05 -0300 Subject: effects: a pty write's bytes are one constant, effect_write_cap, where four bare 64s spelled them, and it is 256: the union is sized by its 256-byte paths (268 bytes an effect at 64 and at 256, the ring 1097728 bytes either way, measured; a comptime check keeps a write from ever being its largest variant), so a paste or a terminal's answer takes a quarter of the effects and of the hosts' write syscalls; the four tests that overflow the ring paste 1.5 MiB where 300 KiB no longer filled it Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/pardes.zig | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'src/pardes.zig') diff --git a/src/pardes.zig b/src/pardes.zig index 2b3ab796..11cb762a 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -3083,13 +3083,17 @@ pub const PtySignal = enum(u8) { int, term, hup, quit, kill }; /// queued values with no lifetime ties back into the core. pub const WatchMode = enum { reconcile, baseline_disk }; pub const effect_path_cap = 256; +/// The bytes one `write` carries, a pty write in the hosts: as many as a +/// path's, which already size the union, so they cost the ring no room +/// (268 bytes an effect at 64 and at 256). +pub const effect_write_cap = 256; pub const Effect = union(enum) { /// A child for the pane in slot `pane` -- the one whose serial is /// `serial`: a pane closed and its slot taken before the effect is /// performed is not the one it was for (perform). spawn: struct { pane: u8, serial: u32, cwd: Buf(effect_path_cap) }, - write: struct { pane: u8, bytes: Buf(64) }, + write: struct { pane: u8, bytes: Buf(effect_write_cap) }, resize_pty: struct { pane: u8, cols: u16, rows: u16 }, /// For the pane whose serial is `serial`, as `spawn` is (perform). signal_pty: struct { pane: u8, serial: u32, sig: PtySignal }, @@ -3146,6 +3150,12 @@ pub const Effect = union(enum) { } }; +comptime { + // A write is never the union's largest variant, so its bytes cost the + // ring of effects no room (effect_write_cap). + std.debug.assert(@sizeOf(@FieldType(Effect, "write")) <= @sizeOf(@FieldType(Effect, "spawn"))); +} + test "a new column takes width only from the column that created it" { if (platform == .web) return; const p = try Pardes.init(std.testing.allocator, .{ .cols = 124, .rows = 24 }); @@ -4823,7 +4833,7 @@ pub const Pardes = struct { // new bytes queue BEHIND it — emitting them now would reorder the pty. if (p.pending_write[id] == null) { while (off < bytes.len and p.effects_len < p.effects.len) { - const n = @min(bytes.len - off, 64); + const n = @min(bytes.len - off, effect_write_cap); p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from(bytes[off .. off + n]) } }); off += n; } @@ -4836,7 +4846,7 @@ pub const Pardes = struct { pub fn writeRoom(p: *const Pardes, id: usize) usize { const pending = limits.pending_write_cap -| p.pending_write_bytes; if (p.pending_write[id] != null) return pending; - return (p.effects.len - p.effects_len) * 64 + pending; + return (p.effects.len - p.effects_len) * effect_write_cap + pending; } fn parkPendingWrite(p: *Pardes, id: usize, bytes: []const u8) void { @@ -5154,7 +5164,7 @@ pub const Pardes = struct { // `|*pw|` points INTO the slot: capturing by value would advance // a copy's cursor and re-send the same chunk for ever. const pw = if (slot.*) |*live| live else break; - const n = @min(pw.bytes.len - pw.off, 64); + const n = @min(pw.bytes.len - pw.off, effect_write_cap); p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from(pw.bytes[pw.off..][0..n]) } }); pw.off += n; p.pending_write_bytes -= n; @@ -8306,12 +8316,12 @@ test "a paste larger than the effect ring reaches the program whole and in order const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - const buf = try gpa.alloc(u8, 1 << 20); + const buf = try gpa.alloc(u8, 2 << 20); defer gpa.free(buf); _ = test_api.ptyBytes(p, null, buf); panes.terminal.enterTty(p, 0); - const text = try gpa.alloc(u8, 300 * 1024); + const text = try gpa.alloc(u8, 1536 * 1024); defer gpa.free(text); for (text, 0..) |*c, i| c.* = 'a' + @as(u8, @intCast(i % 26)); p.update(.{ .paste = text }); @@ -8327,14 +8337,14 @@ test "a bracketed paste larger than the ring still closes its bracket" { const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - const buf = try gpa.alloc(u8, 1 << 20); + const buf = try gpa.alloc(u8, 2 << 20); defer gpa.free(buf); _ = test_api.ptyBytes(p, null, buf); panes.terminal.enterTty(p, 0); p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?2004h" } }); try std.testing.expect(panes.terminal.bracketedPaste(p.panes[0].?)); - const text = try gpa.alloc(u8, 300 * 1024); + const text = try gpa.alloc(u8, 1536 * 1024); defer gpa.free(text); @memset(text, 'z'); p.update(.{ .paste = text }); @@ -8351,12 +8361,12 @@ test "a terminal's answer to its program waits behind the paste still queued for const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - const buf = try gpa.alloc(u8, 1 << 20); + const buf = try gpa.alloc(u8, 2 << 20); defer gpa.free(buf); _ = test_api.ptyBytes(p, null, buf); panes.terminal.enterTty(p, 0); - const text = try gpa.alloc(u8, 300 * 1024); + const text = try gpa.alloc(u8, 1536 * 1024); defer gpa.free(text); @memset(text, 'p'); p.update(.{ .paste = text }); @@ -8378,12 +8388,12 @@ test "pasted bytes still queued at shutdown are freed, not leaked" { const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - const buf = try gpa.alloc(u8, 1 << 20); + const buf = try gpa.alloc(u8, 2 << 20); defer gpa.free(buf); _ = test_api.ptyBytes(p, null, buf); panes.terminal.enterTty(p, 0); - const text = try gpa.alloc(u8, 300 * 1024); + const text = try gpa.alloc(u8, 1536 * 1024); defer gpa.free(text); @memset(text, 'q'); p.update(.{ .paste = text }); -- cgit v1.3