From 411ac49dc8b7d901385caa05d1aefd4843de2a97 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 21:09:59 -0300 Subject: screen-state replicas: a flood's rows renumbered in place, not grown and trimmed by the thousand (a yes delta applies in 97 us, was 2.4 ms), holes blanked when they come into view, and input into a terminal left to the authority screen_state.zig: when a delta's scroll is more than the replica's screen and history hold, nothing it holds is in the new state; its rows stay where they are, renumbered, the active ones and the tail written from the delta, the rest holes, and only as many more rows grown as the authority's history is longer. A hole has whatever that row held before, so `blank` clears the holes in view before they are asked for. The bench's floods run under the C allocator, as a replica does. replica.zig: a replica's input whose live pane is a terminal is not speculated (its step reads the emulator, the last delta's here and the pty's newer one there): it waits for the authority, as a key in tty mode already did at its pty write. ReleaseFast, 200x60 yes at 16 MB/s: apply p50 97 us, p99 284 us; encode a frame 41 us at one peer and at three; 64 KB/s a peer. Co-Authored-By: Claude Opus 5.5 --- src/replica.zig | 6 +++++ src/screen_state.zig | 76 +++++++++++++++++++++++++++++++++++++++------------- test/replicas.zig | 8 +++--- 3 files changed, 69 insertions(+), 21 deletions(-) diff --git a/src/replica.zig b/src/replica.zig index e229bad5..a18391d4 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -622,6 +622,11 @@ pub const Replica = struct { const t0 = nowNs(); tape.beginSpeculation(&r.t); c.toView(r.view); + // Input into a terminal is the authority's: its step reads the + // emulator (rows, cursor, modes), which here is the last delta's and + // there what the pty has said since. It waits; phase 6 predicts the + // echo. + if (r.t.screen_state) if (c.panes[c.active]) |pane| if (pane.isTerminal()) tape.poison(); c.update(in.event); c.pump(c.host) catch {}; const clean = tape.endSpeculation(&r.t); @@ -728,6 +733,7 @@ pub const Replica = struct { sc.rep.missing(shown[0], shown[1], &holes, r.gpa) catch return null; if (holes.items.len == 0) continue; const want: [2]u64 = .{ holes.items[0][0], holes.items[holes.items.len - 1][1] }; + sc.rep.blank(&state.vt, want[0], want[1]); sc.asked = want; var range: [16]u8 = undefined; std.mem.writeInt(u64, range[0..8], want[0], .little); diff --git a/src/screen_state.zig b/src/screen_state.zig index 20ed7ad7..e91f1e14 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -518,13 +518,27 @@ pub const Replica = struct { const primary = t.screens.get(.primary).?; if (top > r.top) { const k = top - r.top; - try scrollUp(primary, @min(k, @as(u64, t.rows) + history)); const held = &r.held[0]; - if (k >= held.items.len) held.clearRetainingCapacity() else { - std.mem.copyForwards(Hash, held.items, held.items[@intCast(k)..]); - held.shrinkRetainingCapacity(held.items.len - @as(usize, @intCast(k))); + if (k >= @as(u64, t.rows) + history) { + // A flood: nothing held here is in this state. The rows stay + // where they are, renumbered: the active ones written below, + // the tail too, the rest holes (blanked when they come into + // view, `blank`), not grown and trimmed by the thousand. As + // many more as the history is longer there, grown blank. + const have = primary.pages.total_rows - primary.pages.rows; + if (have < history) try scrollUp(primary, history - have); + held.clearRetainingCapacity(); + try held.appendNTimes(gpa, unknown, t.rows); + r.holes.clearRetainingCapacity(); + if (history > tail) try r.holes.append(gpa, .{ top - history, top - tail }); + } else { + try scrollUp(primary, @min(k, @as(u64, t.rows) + history)); + if (k >= held.items.len) held.clearRetainingCapacity() else { + std.mem.copyForwards(Hash, held.items, held.items[@intCast(k)..]); + held.shrinkRetainingCapacity(held.items.len - @as(usize, @intCast(k))); + } + if (top - tail > r.top) try r.holes.append(gpa, .{ r.top, top - tail }); } - if (top - tail > r.top) try r.holes.append(gpa, .{ r.top, top - tail }); r.top = top; } // The history as long as the authority's (it prunes a page at a @@ -542,8 +556,8 @@ pub const Replica = struct { const screen = t.screens.active; const held = &r.held[@intFromEnum(t.screens.active_key)]; - const blank = blankHash(t.cols); - while (held.items.len < nrows) try held.append(gpa, blank); + const blank_row = blankHash(t.cols); + while (held.items.len < nrows) try held.append(gpa, blank_row); held.shrinkRetainingCapacity(nrows); for (0..nrows) |y| { const h = std.mem.readInt(u64, hashes[y * 8 ..][0..8], .little); @@ -581,6 +595,19 @@ pub const Replica = struct { return .{ .seq = seq, .extra = extra }; } + /// The holes in `[lo, hi)` blank: until their rows come, a hole shows + /// nothing rather than what that row held before a flood renumbered it. + pub fn blank(r: *const Replica, t: *Terminal, lo: u64, hi: u64) void { + const primary = t.screens.get(.primary).?; + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + for (r.holes.items) |hole| { + const a = @max(lo, hole[0], oldest); + const b = @min(hi, hole[1], r.top); + if (a >= b) continue; + primary.clearRows(.{ .screen = .{ .y = @intCast(a - oldest) } }, .{ .screen = .{ .y = @intCast(b - 1 - oldest) } }, false); + } + } + /// The holes in `[lo, hi)`: the rows to fetch before showing them. pub fn missing(r: *const Replica, lo: u64, hi: u64, out: *std.ArrayList([2]u64), gpa: std.mem.Allocator) !void { for (r.holes.items) |hole| { @@ -679,6 +706,9 @@ fn scrollUp(screen: *Screen, k: u64) !void { screen.cursorReload(); } +/// No row's address: what a row whose content is not known is held as. +const unknown: Hash = std.math.maxInt(Hash); + /// `rowHash` of a row never written. fn blankHash(cols: u16) Hash { var h = std.hash.Wyhash.init(0); @@ -955,6 +985,7 @@ pub const Soak = struct { const hole = s.rep.holes.items[random.uintLessThan(usize, s.rep.holes.items.len)]; const lo = hole[0] + random.uintLessThan(u64, hole[1] - hole[0]); const hi = @min(hole[1], lo + 1 + random.uintLessThan(u64, 80)); + s.rep.blank(s.b, lo, hi); var out: std.Io.Writer.Allocating = .init(s.gpa); defer out.deinit(); try encodeRows(&s.enc, &s.track, s.a, lo, hi, &out.writer); @@ -1077,27 +1108,36 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { // Floods: a second of output as fast as ghostty takes it, a frame every // 16 ms: one version, a delta for each peer (the same bytes for peers - // at the same base), each replica applying its own at once. + // at the same base), each replica applying its own at once. Under the C + // allocator, as a replica's (the checking one costs an apply 4x). + try floods(gpa, std.heap.c_allocator, io, w); +} + +fn floods(gpa: std.mem.Allocator, ca: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { + var enc: Encoder = .init(ca); + defer enc.deinit(); + var out: std.Io.Writer.Allocating = .init(ca); + defer out.deinit(); const yes = try yesStream(gpa, 1 << 16); defer gpa.free(yes); const shell = try shellStream(gpa); defer gpa.free(shell); for ([_]struct { name: []const u8, bytes: []const u8 }{ .{ .name = "yes", .bytes = yes }, .{ .name = "varied text", .bytes = shell } }) |flood| for ([_]usize{ 1, 3 }) |npeers| { - const a = try gpa.create(Terminal); - defer gpa.destroy(a); - a.* = try Terminal.init(io, gpa, .{ .cols = 200, .rows = 60, .max_scrollback_bytes = 16 << 20 }); - defer a.deinit(gpa); + const a = try ca.create(Terminal); + defer ca.destroy(a); + a.* = try Terminal.init(io, ca, .{ .cols = 200, .rows = 60, .max_scrollback_bytes = 16 << 20 }); + defer a.deinit(ca); var as = a.vtStream(); defer as.deinit(); - var tr: Track = .init(gpa); - defer tr.deinit(gpa, a); + var tr: Track = .init(ca); + defer tr.deinit(ca, a); var peers: [3]Peer = @splat(.{}); var reps: [3]Replica = @splat(.{}); var bs: [3]Terminal = undefined; - for (bs[0..npeers]) |*b| b.* = try Terminal.init(io, gpa, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 32 << 20 }); + for (bs[0..npeers]) |*b| b.* = try Terminal.init(io, ca, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 32 << 20 }); defer for (bs[0..npeers], reps[0..npeers]) |*b, *r| { - b.deinit(gpa); - r.deinit(gpa); + b.deinit(ca); + r.deinit(ca); }; var applies: std.ArrayList(u64) = .empty; defer applies.deinit(gpa); @@ -1124,7 +1164,7 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { if (!try encode(&enc, &tr, peer, a, version, "", &out.writer)) continue; frame_ns += @intCast(std.Io.Clock.awake.now(io).nanoseconds - e0); const a0 = std.Io.Clock.awake.now(io).nanoseconds; - peer.ack((try rep.apply(gpa, io, b, out.written())).seq); + peer.ack((try rep.apply(ca, io, b, out.written())).seq); try applies.append(gpa, @intCast(std.Io.Clock.awake.now(io).nanoseconds - a0)); deltas += 1; wire += out.written().len; diff --git a/test/replicas.zig b/test/replicas.zig index f3618cf1..1acd1c11 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -383,8 +383,10 @@ fn run(r: Run, dir: []const u8, totals: *Totals) !void { const t0 = screens.encode_ns; const before = totals.delta_bytes; try flushAll(&screens, a.core, peers[0..r.clients], &owed, &queue, &n, m.at, &down, random, one_way, jitter, &versions, totals); - if (screens.encode_ns > t0) try totals.frame_ns.append(gpa, screens.encode_ns - t0); - if (m.at >= flood_at and (flood_end == 0 or m.at <= flood_end)) totals.flood_delta_bytes += totals.delta_bytes - before; + if (m.at >= flood_at and (flood_end == 0 or m.at <= flood_end)) { + totals.flood_delta_bytes += totals.delta_bytes - before; + try totals.frame_ns.append(gpa, screens.encode_ns - t0); + } // Frames go on while anything moves, and a second after. const busy = typing > 0 or flood_end == 0 or queue.count() > 0; frames_left = if (busy) 60 else frames_left -| 1; @@ -537,7 +539,7 @@ pub fn main(init: std.process.Init) !void { }); if (t.divergences > 0 or t.unequal > 0 or t.screens_wrong > 0) failed = true; } - std.debug.print("terminals: deltas a replica applied a second under the flood; a peer's bytes a second under the flood (deltas, units); encode a frame for every peer, apply a delta (us); rows fetched; resets; deltas checked/wrong\n", .{}); + std.debug.print("terminals: deltas a replica applied a second under the flood; a peer's bytes a second under the flood (deltas, units); encode a frame of the flood for every peer, apply a delta (us); rows fetched; resets; deltas checked/wrong\n", .{}); std.debug.print(" rtt deltas/s delta B/s unit B/s encode p50/p99 apply p50/p99 fetched resets checked/wrong\n{s}", .{term_lines.items}); if (failed) std.process.exit(1); } -- cgit v1.3