From 370aaefe45a338c76a0e2d1a2a0a52d7d3e2f060 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:51:08 -0300 Subject: screen_state: a one-row scratch page is scratchPage's and holes past a history's oldest row are clampHoles's, each written twice before (the encoder's and the replica's, the peer's and the replica's), and a test's io helper is inlined Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/screen_state.zig | 67 +++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 37 deletions(-) (limited to 'src/screen_state.zig') diff --git a/src/screen_state.zig b/src/screen_state.zig index 002c9da7..8ae9149d 100644 --- a/src/screen_state.zig +++ b/src/screen_state.zig @@ -153,14 +153,7 @@ pub const Encoder = struct { var cap: vt.page.Capacity = if (e.scratch) |s| s.capacity else .{ .cols = cols, .rows = 1, .styles = 64 }; if (cap.cols != cols) cap = .{ .cols = cols, .rows = 1, .styles = 64 }; for (0..16) |_| { - if (e.scratch) |*s| { - if (std.meta.eql(s.capacity, cap)) s.reinit() else { - s.deinit(); - e.scratch = null; - } - } - if (e.scratch == null) e.scratch = try Page.init(cap); - const s = &e.scratch.?; + const s = try scratchPage(&e.scratch, cap); s.cloneRowFrom(page, s.getRow(0), row) catch { // Room for this row: everything doubled until it fits. cap.styles *|= 2; @@ -177,6 +170,31 @@ pub const Encoder = struct { } }; +/// `slot`'s one-row scratch page, ready for `cap`: reused when it has that +/// capacity, else made again. +fn scratchPage(slot: *?Page, cap: vt.page.Capacity) !*Page { + if (slot.*) |*s| { + if (std.meta.eql(s.capacity, cap)) { + s.reinit(); + return s; + } + s.deinit(); + slot.* = null; + } + slot.* = try Page.init(cap); + return &slot.*.?; +} + +/// Holes older than a history's oldest row are gone with it. +fn clampHoles(holes: *std.ArrayList([2]u64), oldest: u64) void { + var i: usize = 0; + while (i < holes.items.len) { + const hole = &holes.items[i]; + hole[0] = @max(hole[0], oldest); + if (hole[0] >= hole[1]) _ = holes.orderedRemove(i) else i += 1; + } +} + /// Deltas a peer may have in flight. At 207 ms and 60 frames a second about /// 13 are; past the bound a peer gets nothing until an acknowledgement, and /// then the newest state, the ones between skipped. @@ -323,13 +341,7 @@ pub const Peer = struct { 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; - } + clampHoles(&peer.holes, top -| history); } }; @@ -726,30 +738,15 @@ pub const Replica = struct { } } - /// Holes older than this history's oldest row are gone with it. fn trimHoles(r: *Replica, primary: *Screen) void { - const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); - var i: usize = 0; - while (i < r.holes.items.len) { - const hole = &r.holes.items[i]; - hole[0] = @max(hole[0], oldest); - if (hole[0] >= hole[1]) _ = r.holes.orderedRemove(i) else i += 1; - } + clampHoles(&r.holes, r.top -| (primary.pages.total_rows - primary.pages.rows)); } fn writeRow(r: *Replica, gpa: std.mem.Allocator, screen: *Screen, p: Pin, blob: []const u8) !void { var reader: std.Io.Reader = .fixed(blob); var dec: snapshot.page.Decoder = undefined; try dec.init(&reader, try snapshot.record.nextHeader(&reader)); - const cap = dec.capacity(); - if (r.scratch) |*s| { - if (std.meta.eql(s.capacity, cap)) s.reinit() else { - s.deinit(); - r.scratch = null; - } - } - if (r.scratch == null) r.scratch = try Page.init(cap); - const src = &r.scratch.?; + const src = try scratchPage(&r.scratch, dec.capacity()); try dec.decode(src, gpa); if (src.size.cols != screen.pages.cols) return error.BadRow; _ = screen.clonePartialRowGrowCapacity(p.node, p.y, src, src.getRow(0), 0, src.size.cols); @@ -1454,7 +1451,7 @@ test "a replica's terminal pane draws as its authority's does" { a.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); delta.clearRetainingCapacity(); try testing.expect(try encode(&enc, &tr, &peer, ta, @intCast(i + 1), "", &delta.writer)); - peer.ack((try rep.apply(gpa, terminalIoForTest(), tb, delta.written())).seq); + peer.ack((try rep.apply(gpa, std.Io.Threaded.global_single_threaded.io(), tb, delta.written())).seq); b.needs_frame = true; _ = fa.reset(.retain_capacity); _ = fb.reset(.retain_capacity); @@ -1472,7 +1469,3 @@ test "a replica's terminal pane draws as its authority's does" { }; } } - -fn terminalIoForTest() std.Io { - return std.Io.Threaded.global_single_threaded.io(); -} -- cgit v1.3