diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/replica.zig | 6 | ||||
| -rw-r--r-- | src/screen_state.zig | 76 |
2 files changed, 64 insertions, 18 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; |
