diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 21:45:19 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 21:45:19 -0300 |
| commit | 473bbaa838f290e932992ef625be2fdc05c952dc (patch) | |
| tree | 3165ffa9af3e9c0c3e777d012228cc033c1bbf80 /src/screen_state.zig | |
| parent | 64184a16e58094f6b861cd1027920ddf21d9df0d (diff) | |
| download | pardes-473bbaa838f290e932992ef625be2fdc05c952dc.tar.gz pardes-473bbaa838f290e932992ef625be2fdc05c952dc.zip | |
screen-state replicas: what a terminal's ingest tells the positions pardes keeps over it taped, the history a step reads sent before its unit, rows sent a blob once a message, and the rows cache out of the rollback copy
terminal.zig, tape.zig: ingestWithPositions tracks its rows only where an ingest happens; what the ingest told them (each tracked row's row after it and whether it was erased, a clear, whether the pages lived, a pin not made) is a taped value (Site.positions), which a screen-state replica, ingesting nothing, takes; the logic after it runs alike on both. On the stack, so the local path allocates nothing new. buildRows marks a terminal read (State.read) outside the render (terminal.drawing).
screen_state.zig, replica.zig: the authority keeps each peer's holes as its replica does (Peer.holes, from the deltas it sent), and Screens.fill sends a peer the rows it lacks of every terminal a step read, after the step and before its unit, so a replay reads what the step read (a text-mode cursor moved into history the flood scrolled past diverged). encodeRows sends each distinct blob once a message and the rows as addresses (a fill after yes is a third). The rows cache is dropped when a copy is taken and emptied when it is put back, not saved.
test/replicas.zig: Ctrl-b in the mix (a shell out of tty mode: text, cursors and selections over it), and a shell's session (prompts, titles, directories, clears) between the TUI and the flood. 4 seeds x 120 inputs x 3 clients at 30, 80, 207 ms: no divergence, every replica and terminal the authority's, 4,653 deltas checked.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/screen_state.zig')
| -rw-r--r-- | src/screen_state.zig | 98 |
1 files changed, 81 insertions, 17 deletions
diff --git a/src/screen_state.zig b/src/screen_state.zig index e91f1e14..27e20c0f 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -191,8 +191,10 @@ const Known = struct { pub const Track = struct { rows: Rows = .{}, version: u32 = 0, - /// The emulator's change count the newest version was made at. + /// The emulator's change count the newest version was made at, and its + /// history's rows. changes: u32 = 0, + history: u64 = 0, /// The newest version: its active rows (pins, good while the emulator /// is unchanged) and their addresses, and its meta. pins: std.ArrayList(Pin) = .empty, @@ -236,6 +238,8 @@ pub const Track = struct { tr.version +%= 1; if (tr.version == 0) tr.version = 1; tr.changes = changes; + const primary = &t.screens.get(.primary).?.pages; + tr.history = primary.total_rows - primary.rows; tr.meta.clearRetainingCapacity(); { var rw: snapshot.record.Writer = .init(gpa, &tr.meta.writer); @@ -271,6 +275,15 @@ pub const Peer = struct { /// The versions in flight, oldest first. flight: [bound]u32 = undefined, n: u8 = 0, + /// The history rows its replica holds as holes, absolute, as the + /// replica keeps them (Replica.apply's rule, from what it was sent): + /// what `fill` sends before a step reads them. + top: u64 = 0, + holes: std.ArrayListUnmanaged([2]u64) = .empty, + + pub fn deinit(peer: *Peer, gpa: std.mem.Allocator) void { + peer.holes.deinit(gpa); + } /// Every version up to `seq` has landed. pub fn ack(peer: *Peer, seq: u32) void { @@ -282,7 +295,36 @@ pub const Peer = struct { /// Start the peer afresh: it lost its state, or never had one. pub fn reset(peer: *Peer) void { - peer.* = .{}; + var holes = peer.holes; + holes.clearRetainingCapacity(); + peer.* = .{ .holes = holes }; + } + + /// Its replica's holes once it applies a delta to `tr`'s newest version + /// against `base`, as Replica.apply makes them. + fn noteSent(peer: *Peer, gpa: std.mem.Allocator, tr: *const Track, base: ?*const Known, rows: u16) !void { + const top = tr.rows.top; + const history = tr.history; + if (base) |b| { + if (top > peer.top) { + const tail = @min(top -| b.top, rows, history); + if (top - peer.top >= @as(u64, rows) + history) { + peer.holes.clearRetainingCapacity(); + if (history > tail) try peer.holes.append(gpa, .{ top - history, top - tail }); + } else if (top - tail > peer.top) try peer.holes.append(gpa, .{ peer.top, top - tail }); + } + } else { + peer.holes.clearRetainingCapacity(); + if (history > 0) try peer.holes.append(gpa, .{ top - history, top }); + } + peer.top = top; + // Those older than its history's oldest row are gone with it. + var i: usize = 0; + while (i < peer.holes.items.len) { + const hole = &peer.holes.items[i]; + hole[0] = @max(hole[0], top -| history); + if (hole[0] >= hole[1]) _ = peer.holes.orderedRemove(i) else i += 1; + } } }; @@ -317,6 +359,7 @@ pub fn encode(e: *Encoder, tr: *Track, peer: *Peer, t: *Terminal, changes: u32, tr.cache_base = base_version; } try out.writeAll(tr.cache.written()); + try peer.noteSent(e.gpa, tr, base, t.rows); peer.sent = tr.version; peer.flight[peer.n] = tr.version; peer.n += 1; @@ -389,30 +432,41 @@ fn needBlob(e: *Encoder, base: ?*const Known, p: Pin, h: Hash) !void { try e.needed.append(e.gpa, .{ .pin = p, .hash = h }); } -/// The primary screen's rows `[lo, hi)` the authority still has, as u32 -/// epoch, u32 count, then each as u64 row, u32 length, blob: a replica's -/// fetch of history it holds as holes. -// ponytail: every row its blob, none by address: phase 6's content cache. +/// The primary screen's rows `[lo, hi)` the authority still has, for a +/// replica that holds them as holes: u32 epoch, u32 blobs, each u64 hash, +/// u32 length, bytes, then u32 rows, each u64 row and u64 hash. A blob goes +/// once a message (a flood's rows are one). +// ponytail: blobs once a message, not once a peer: phase 6's content cache. pub fn encodeRows(e: *Encoder, tr: *Track, t: *Terminal, lo: u64, hi: u64, out: *std.Io.Writer) !void { try tr.rows.update(t); var blobs: std.Io.Writer.Allocating = .init(e.gpa); defer blobs.deinit(); - var count: u32 = 0; + var rows: std.Io.Writer.Allocating = .init(e.gpa); + defer rows.deinit(); + e.listed.clearRetainingCapacity(); + var nblobs: u32 = 0; + var nrows: u32 = 0; var abs = lo; // The active area is the deltas'. while (abs < @min(hi, tr.rows.top)) : (abs += 1) { const p = tr.rows.pinAt(t, abs) orelse continue; - const start = blobs.written().len; - try blobs.writer.writeInt(u64, abs, .little); - try blobs.writer.writeInt(u32, 0, .little); - try e.rowBlob(p.node.page(), p.rowAndCell().row, t.cols, &blobs.writer); - const len: u32 = @intCast(blobs.written().len - start - 12); - std.mem.writeInt(u32, blobs.written()[start + 8 ..][0..4], len, .little); - count += 1; + const h = rowHash(p.node.page(), p.rowAndCell().row, t.cols); + try rows.writer.writeInt(u64, abs, .little); + try rows.writer.writeInt(u64, h, .little); + nrows += 1; + if ((try e.listed.getOrPut(e.gpa, h)).found_existing) continue; + e.blob.clearRetainingCapacity(); + try e.rowBlob(p.node.page(), p.rowAndCell().row, t.cols, &e.blob.writer); + try blobs.writer.writeInt(u64, h, .little); + try blobs.writer.writeInt(u32, @intCast(e.blob.written().len), .little); + try blobs.writer.writeAll(e.blob.written()); + nblobs += 1; } try out.writeInt(u32, tr.rows.epoch, .little); - try out.writeInt(u32, count, .little); + try out.writeInt(u32, nblobs, .little); try out.writeAll(blobs.written()); + try out.writeInt(u32, nrows, .little); + try out.writeAll(rows.written()); } /// A replica's terminal: what it was sent, written into a ghostty Terminal. @@ -631,9 +685,15 @@ pub const Replica = struct { var in: std.Io.Reader = .fixed(bytes); if (try in.takeInt(u32, .little) != r.epoch) return; // numbered before a resize const primary = t.screens.get(.primary).?; + var blobs: std.AutoHashMapUnmanaged(Hash, []const u8) = .empty; + defer blobs.deinit(gpa); + for (0..try in.takeInt(u32, .little)) |_| { + const h = try in.takeInt(u64, .little); + try blobs.put(gpa, h, try in.take(try in.takeInt(u32, .little))); + } for (0..try in.takeInt(u32, .little)) |_| { const abs = try in.takeInt(u64, .little); - const blob = try in.take(try in.takeInt(u32, .little)); + const blob = blobs.get(try in.takeInt(u64, .little)) orelse return error.MissingBlob; // Only a hole is written: a row the replica holds is the // authority's already, and newer than what this fetch saw. if (abs >= r.top or !r.inHole(abs)) continue; @@ -852,6 +912,7 @@ pub const Soak = struct { s.b.deinit(gpa); gpa.destroy(s.b); s.enc.deinit(); + s.peer.deinit(gpa); s.rep.deinit(gpa); for (s.flight.items) |f| { gpa.free(f.delta); @@ -1088,6 +1149,7 @@ fn bench(gpa: std.mem.Allocator, io: std.Io, w: *std.Io.Writer) !void { var tr: Track = .init(gpa); defer tr.deinit(gpa, t); var peer: Peer = .{}; + defer peer.deinit(gpa); out.clearRetainingCapacity(); var t0 = std.Io.Clock.awake.now(io).nanoseconds; _ = try encode(&enc, &tr, &peer, t, changes, "", &out.writer); @@ -1135,9 +1197,10 @@ fn floods(gpa: std.mem.Allocator, ca: std.mem.Allocator, io: std.Io, w: *std.Io. var reps: [3]Replica = @splat(.{}); var bs: [3]Terminal = undefined; 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| { + defer for (bs[0..npeers], reps[0..npeers], peers[0..npeers]) |*b, *r, *peer| { b.deinit(ca); r.deinit(ca); + peer.deinit(ca); }; var applies: std.ArrayList(u64) = .empty; defer applies.deinit(gpa); @@ -1348,6 +1411,7 @@ test "a replica's terminal pane draws as its authority's does" { var enc: Encoder = .init(gpa); defer enc.deinit(); var peer: Peer = .{}; + defer peer.deinit(gpa); var tr: Track = .init(gpa); defer tr.deinit(gpa, ta); var rep: Replica = .{}; |
