summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 18:31:03 -0300
committerGabriel Schneider <[email protected]>2026-11-06 20:00:43 -0300
commit3e1677f526baa02179c5646223f48b316e5152e0 (patch)
tree2ac232f703a0c2f17abc0f07db4161660c0c2544 /src
parent1b38ca0aa0bfae1177f80a6713ec6d787d2f77d4 (diff)
downloadpardes-3e1677f526baa02179c5646223f48b316e5152e0.tar.gz
pardes-3e1677f526baa02179c5646223f48b316e5152e0.zip
kinds #40, rasters as bounded messages on the wire (§17.3 C): a placement over PA_RASTER_MAX is dropped at paint and said once, a kind's frame names its placements by key and their pixels cross before it as `raster` strips of at most 4 MiB over the rows its `src` shows (only those the peer was not sent with the frame before), the peer assembles them by name in a 64 MiB cache let go by least recent use but for what a Shadow shows, a frame naming one it lacks draws its cells and asks once for its pane again, the sender checks every message against wire.max_payload (Debug's panic, release's drop and log) and a reader names an oversized one (said on the client as it joins again, logged at the session) instead of reading it as the link's end, so kinds #21's byte budget goes; wire 23; and pdf.c lays out for a view with pictures over a glyph view until an input chooses (one layout until 7c), so the 110-column window of the stall's repro shows its page
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
-rw-r--r--src/Kind.zig17
-rw-r--r--src/replica.zig92
-rw-r--r--src/screen_state.zig315
-rw-r--r--src/wire.zig6
4 files changed, 286 insertions, 144 deletions
diff --git a/src/Kind.zig b/src/Kind.zig
index 71ff4890..db3bee49 100644
--- a/src/Kind.zig
+++ b/src/Kind.zig
@@ -392,11 +392,6 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa
};
for (frame.places[0..frame.n]) |pl| {
if (pl.rgba == null or pl.w == 0 or pl.h == 0) continue;
- if (@as(u64, pl.w) * pl.h * 4 > c.PA_RASTER_MAX) {
- if (slot.over & 1 << 31 == 0) std.log.warn("{s}: a picture of {d} MB, over {d} MiB", .{ slot.nameOf(), @as(u64, pl.w) * pl.h * 4 >> 20, c.PA_RASTER_MAX >> 20 });
- slot.over |= 1 << 31;
- continue;
- }
const dx = @min(pl.dst.x, cols);
const dy = @min(pl.dst.y, rows);
_ = p.appendPlacement(.{
@@ -439,7 +434,17 @@ pub fn paint(p: *const Pardes, slot: *Slot, id: usize, arena: std.mem.Allocator,
if (checked and usedHash(slot) != before) std.debug.panic("addon {s}: paint changed its state (paint must be pure)", .{slot.nameOf()});
_ = cl.end(prev);
frame.places = &frame.buf;
- frame.n = @min(out.n, frame.buf.len);
+ // A picture over the bound is dropped, said once a pane (17.3).
+ frame.n = 0;
+ for (frame.buf[0..@min(out.n, frame.buf.len)]) |pl| {
+ if (@as(u64, pl.w) * pl.h * 4 > c.PA_RASTER_MAX) {
+ if (slot.over & 1 << 31 == 0) std.log.warn("{s}: a picture of {d} MB, over {d} MiB", .{ slot.nameOf(), @as(u64, pl.w) * pl.h * 4 >> 20, c.PA_RASTER_MAX >> 20 });
+ slot.over |= 1 << 31;
+ continue;
+ }
+ frame.buf[frame.n] = pl;
+ frame.n += 1;
+ }
slot.thumb_at = std.math.clamp(out.at, 0, 1);
slot.thumb_span = std.math.clamp(out.span, 0, 1);
slot.pointer = @intCast(@min(out.pointer, 255));
diff --git a/src/replica.zig b/src/replica.zig
index 29904fa0..5b2facb4 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -299,10 +299,13 @@ pub const Authority = struct {
// to a replica: kind 1, a delta; kind 2, rows (encodeRows): asked for,
// or a step read them; kind 4, a pane of a kind's frame
// (a Shadow, screen_state.encodeShadow), at the size the
-// peer's view gives the pane;
+// peer's view gives the pane; kind 5, a strip of one of
+// its rasters (encodeStrip), before the frame naming it;
// to the authority: kind 1, an acknowledgement (u32 seq); kind 2, a
// request for rows (u64 lo, u64 hi, absolute); kind 3, a
-// delta refused: start this terminal afresh.
+// delta refused: start this terminal afresh; kind 4, a
+// kind's frame naming a raster the replica lacks: all
+// of that pane again.
// In the authority's stream to a peer, before it steps any input but a
// pty's output, and once a frame: its own steps since the last unit, as one
// (`Authority.pending`), then what the peer is owed of the terminals
@@ -314,8 +317,10 @@ pub const Authority = struct {
const kind_delta = 1;
const kind_rows = 2;
const kind_shadow = 4;
+const kind_raster = 5;
const kind_ack = 1;
const kind_reset = 3;
+const kind_want = 4;
const ScreenMsg = struct { pane: u8, serial: u32, kind: u8, payload: []const u8 };
@@ -442,14 +447,26 @@ pub const Screens = struct {
if (cols == 0 or rows == 0) continue;
var frame: pardes.Kind.Frame = .{ .cols = cols, .rows = rows };
frame.cells = pardes.Kind.paint(p, k, id, arena.allocator(), cols, rows, &frame) orelse continue;
+ // Its rasters' strips over what it shows that the peer
+ // lacks, each a message before the frame naming them.
+ for (frame.places[0..@min(frame.n, 64)]) |*pl| {
+ // A row past a strip's bound cannot cross: not shown there.
+ if (@as(usize, pl.w) * 4 > screen_state.strip_max) continue;
+ var owed = try pp.shadow.owed(x.s.gpa, pl);
+ while (owed != 0) : (owed &= owed - 1) {
+ x.s.out.clearRetainingCapacity();
+ try writeScreenHead(&x.s.out, x.s.gpa, @intCast(id), pane.serial, kind_raster);
+ var w: std.Io.Writer.Allocating = .fromArrayList(x.s.gpa, &x.s.out);
+ defer x.s.out = w.toArrayList();
+ try screen_state.encodeStrip(pl, @ctz(owed), &w.writer);
+ try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written()));
+ }
+ }
x.s.out.clearRetainingCapacity();
try writeScreenHead(&x.s.out, x.s.gpa, @intCast(id), pane.serial, kind_shadow);
var w: std.Io.Writer.Allocating = .fromArrayList(x.s.gpa, &x.s.out);
defer x.s.out = w.toArrayList();
- // Within the wire's bound, its head (6 bytes) aside: a page of a
- // PDF in a wide window is more, and a message past it ends
- // the peer's link (its reader stops at it).
- if (!try screen_state.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.pointer, wire.max_payload - 6, &w.writer)) continue;
+ if (!try screen_state.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.pointer, &w.writer)) continue;
try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written()));
};
}
@@ -512,6 +529,8 @@ pub const Screens = struct {
const m = parseScreen(msg) orelse return null;
const pane = core.panes[m.pane] orelse return null;
if (pane.serial != m.serial) return null;
+ // A kind's frame whose raster the peer lacks: its pane sent afresh.
+ if (m.kind == kind_want and pane.addon != null) s.peers.items[peer][m.pane].shadow.deinit(s.gpa);
const state = pane.terminal orelse return null;
const pp = &s.peers.items[peer][m.pane];
if (pp.serial != m.serial) return null;
@@ -659,6 +678,8 @@ pub const Replica = struct {
/// speculation may move a view, so a rollback puts these back.
views: [pardes.MAX_PANES]View = @splat(.{}),
screen_out: std.ArrayListUnmanaged(u8) = .empty,
+ /// Its Shadows' rasters, from the session's strips (17.3).
+ blobs: screen_state.Blobs = .{},
/// Its predicted echo (src/echo.zig), drawn over its core's terminals:
/// fed by its keys, its units and its deltas here, and by `echoFrame`.
echo: pardes.echo.Echo = .{},
@@ -744,6 +765,7 @@ pub const Replica = struct {
c.deinit();
was.end();
}
+ r.blobs.deinit(r.gpa);
r.copy.deinit();
for (r.pending.items) |p| r.freePending(p);
r.pending.deinit(r.gpa);
@@ -1109,6 +1131,14 @@ pub const Replica = struct {
const c = r.t.core orelse return null;
const pane = c.panes[m.pane] orelse return null;
if (pane.serial != m.serial) return null; // a pane gone: its slot's next is another
+ // A strip of a raster of a pane of a kind, kept until a frame shows it.
+ if (m.kind == kind_raster and m.payload.len >= 8) {
+ var shown: std.ArrayList([*]const u8) = .empty;
+ defer shown.deinit(r.gpa);
+ for (c.panes) |slot| if (slot) |p| if (p.addon) |k| if (k.shadow) |sh| for (sh.places.items) |pl| shown.append(r.gpa, pl.rgba) catch {};
+ r.blobs.strip(r.gpa, std.hash.Wyhash.hash(m.serial, m.payload[0..8]), m.payload, shown.items) catch r.fail("a kind's raster did not apply");
+ return null;
+ }
// A pane of a kind: the authority's frame of it, drawn as it is.
if (m.kind == kind_shadow) if (pane.addon) |k| {
const sh = k.shadow orelse sh: {
@@ -1117,8 +1147,11 @@ pub const Replica = struct {
k.shadow = made;
break :sh made;
};
- sh.apply(k.gpa, m.payload) catch r.fail("a kind's frame did not apply");
- return null;
+ const lacking = sh.apply(k.gpa, m.payload, m.serial, &r.blobs) catch {
+ r.fail("a kind's frame did not apply");
+ return null;
+ };
+ return if (lacking != 0) r.screenMessage(m.pane, m.serial, kind_want, &.{}) else null;
};
const state = pane.terminal orelse return null;
const slot = &r.screens[m.pane];
@@ -1374,6 +1407,13 @@ pub const Framer = struct {
f.z = null;
}
+ /// The length a message past the wire's bound said it had, when that
+ /// is where `run` stopped: an error to name, never a hangup (17.3).
+ pub fn overlong(fr: *const Framer) ?u32 {
+ for ([_][]const u8{ fr.buf.items[0..fr.len], fr.inner.items }) |b| if (wire.framed(b)) |_| {} else |_| return std.mem.readInt(u32, b[1..5], .little);
+ return null;
+ }
+
/// Framed messages off `fd` (wire.zig) handed to `take` until
/// the stream ends, `take` says stop, or a message is malformed. An
/// envelope's are handed on as if they had come bare.
@@ -1627,6 +1667,14 @@ pub const Peer = struct {
peer.lock.lockUncancelable(io);
defer peer.lock.unlock(io);
if (peer.cut or peer.closing) return;
+ // The wire's bound, as the message goes (17.3): one past it would
+ // be the link's end at the reader.
+ const len = std.mem.readInt(u32, parts[0][1..5], .little);
+ if (len > wire.max_payload) {
+ log.err("a {d} MB message to client {d}, over the {d} MiB bound: dropped", .{ len >> 20, peer.id, wire.max_payload >> 20 });
+ if (checked) std.debug.panic("a {d}-byte message to client {d}, over the wire's bound", .{ len, peer.id });
+ return;
+ }
var n: usize = peer.out.items.len;
for (parts) |p| n += p.len;
if (n > peer.limit) {
@@ -1748,6 +1796,7 @@ pub const Peer = struct {
// The framer goes before `gone`, after which the peer may be freed.
var fr: Framer = .{};
fr.run(peer.peers.gpa, peer.fd, peer, take);
+ if (fr.overlong()) |n| log.err("client {d} sent a {d} MB message, over the {d} MiB bound: let go", .{ peer.id, n >> 20, wire.max_payload >> 20 });
fr.deinit(peer.peers.gpa);
peer.peers.post(.{ .gone = peer });
}
@@ -1882,7 +1931,11 @@ pub const Peers = struct {
},
.screen => |in| {
defer ps.gpa.free(in.msg);
- const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue;
+ const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse {
+ // A raster asked for again: its pane's frame now.
+ if (in.msg.len > 5 and in.msg[5] == kind_want) ps.screensTo(core, in.peer, .flush);
+ continue;
+ };
defer ps.gpa.free(reply);
const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len);
in.peer.send(&.{ &head, reply });
@@ -2648,6 +2701,10 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime
}
};
rest.run(gpa, fd, ctx, Take.take);
+ // A message past the wire's bound: said, never a hangup.
+ const n = rest.overlong() orelse return;
+ const words = std.fmt.allocPrint(std.heap.c_allocator, "the session sent a {d} MB message, over the {d} MiB bound", .{ n >> 20, wire.max_payload >> 20 }) catch return;
+ if (!post(ctx, .{ .refused = words })) std.heap.c_allocator.free(words);
}
/// The words a client goes with when its session runs another build now.
@@ -2952,6 +3009,23 @@ pub const Link = struct {
}
};
+test "a message past the wire's bound is named, never read as the link's end" {
+ var fds: [2]c_int = undefined;
+ try std.testing.expectEqual(0, libc.pipe(&fds));
+ const head = wire.header(@backingInt(wire.ServerTag.unit), wire.max_payload + 1);
+ _ = libc.write(fds[1], &head, head.len);
+ _ = libc.close(fds[1]);
+ var fr: Framer = .{};
+ defer fr.deinit(std.testing.allocator);
+ fr.run(std.testing.allocator, fds[0], {}, struct {
+ fn take(_: void, _: u8, _: []const u8) bool {
+ return true;
+ }
+ }.take);
+ _ = libc.close(fds[0]);
+ try std.testing.expectEqual(wire.max_payload + 1, fr.overlong().?);
+}
+
test "a client joining again after a divergence is told at once that its session is gone" {
var why: [600]u8 = undefined;
const t0 = nowNs();
diff --git a/src/screen_state.zig b/src/screen_state.zig
index 70e1614f..7be7d0d2 100644
--- a/src/screen_state.zig
+++ b/src/screen_state.zig
@@ -1298,38 +1298,40 @@ test "a replica's terminal pane draws as its authority's does" {
// A replica's core has no code for a library's kind: such a pane is drawn
// from the authority's frame of it, painted at the size the peer's view
// gives it. A frame crosses as its size and the pointer it asks for, each
-// row by its hash (with its cells when
-// the peer's row there is another), then each placement, with its raster's
-// bytes when the peer was not sent that key with the frame before. The
-// stream is ordered and loses nothing, so what the authority last sent a
-// peer is what the peer holds.
+// row by its hash (with its cells when the peer's row there is another),
+// then each placement by its key; its raster's pixels cross before it, as
+// strips of rows (`raster` messages, `encodeStrip`), those over its `src`
+// the peer was not sent with the frame before. The stream is ordered and
+// loses nothing, so what the authority last sent a peer is what the peer
+// holds, but for a raster its `Blobs` let go, which it asks for again.
const Kind = @import("Kind.zig");
const Cell = Kind.c.pa_cell;
const Place = Kind.c.pa_placement;
-/// A replica's frame of a pane of a kind: what Kind.draw draws for it.
+/// A replica's frame of a pane of a kind: what Kind.draw draws for it, its
+/// placements' pixels its peer's `Blobs`.
pub const Shadow = struct {
cols: u16 = 0,
rows: u16 = 0,
cells: []Cell = &.{},
hashes: []Hash = &.{},
places: std.ArrayList(Place) = .empty,
- /// Each placement's raster, owned, beside it.
- rasters: std.ArrayList([]u8) = .empty,
pointer: u8 = 0,
+ /// The raster it last asked for again (`apply`'s): asked once.
+ wanted: u64 = 0,
pub fn deinit(sh: *Shadow, gpa: std.mem.Allocator) void {
gpa.free(sh.cells);
gpa.free(sh.hashes);
- for (sh.rasters.items) |r| gpa.free(r);
- sh.rasters.deinit(gpa);
sh.places.deinit(gpa);
gpa.destroy(sh);
}
- /// A frame message applied (`encodeShadow`'s).
- pub fn apply(sh: *Shadow, gpa: std.mem.Allocator, bytes: []const u8) !void {
+ /// A frame message applied (`encodeShadow`'s), of the pane `serial`,
+ /// each placement's pixels from `blobs`: one it lacks is not shown, and
+ /// its name is what to ask for (once), else 0.
+ pub fn apply(sh: *Shadow, gpa: std.mem.Allocator, bytes: []const u8, serial: u32, blobs: *Blobs) !u64 {
var r: std.Io.Reader = .fixed(bytes);
const cols = try r.takeInt(u16, .little);
const rows = try r.takeInt(u16, .little);
@@ -1356,18 +1358,8 @@ pub const Shadow = struct {
c.flags = try r.takeByte();
}
}
- var places: std.ArrayList(Place) = .empty;
- errdefer places.deinit(gpa);
- var rasters: std.ArrayList([]u8) = .empty;
- defer {
- for (sh.rasters.items) |old| if (for (rasters.items) |kept| {
- if (kept.ptr == old.ptr) break false;
- } else true) gpa.free(old);
- sh.rasters.deinit(gpa);
- sh.rasters = rasters;
- sh.places.deinit(gpa);
- sh.places = places;
- }
+ sh.places.clearRetainingCapacity();
+ var lacking: u64 = 0;
for (0..try r.takeByte()) |_| {
var pl = std.mem.zeroes(Place);
pl.w = try r.takeInt(u32, .little);
@@ -1375,73 +1367,153 @@ pub const Shadow = struct {
inline for (.{ &pl.src.x, &pl.src.y, &pl.src.w, &pl.src.h, &pl.at.x, &pl.at.y, &pl.at.w, &pl.at.h }) |f| f.* = try r.takeInt(u32, .little);
inline for (.{ &pl.dst.x, &pl.dst.y, &pl.dst.w, &pl.dst.h }) |f| f.* = try r.takeInt(u16, .little);
pl.key = try r.takeInt(u64, .little);
- const raster = if (try r.takeByte() != 0) try gpa.dupe(u8, try r.take(@as(usize, pl.w) * pl.h * 4)) else for (sh.places.items, sh.rasters.items) |old, held| {
- if (old.key == pl.key) break held;
- } else return error.BadShadow;
- pl.rgba = raster.ptr;
- try rasters.append(gpa, raster);
- try places.append(gpa, pl);
+ const name = std.hash.Wyhash.hash(serial, std.mem.asBytes(&pl.key));
+ pl.rgba = (blobs.shows(name, &pl) orelse {
+ lacking = name;
+ continue;
+ }).ptr;
+ try sh.places.append(gpa, pl);
}
+ if (lacking == sh.wanted) return 0;
+ sh.wanted = lacking;
+ return lacking;
+ }
+};
+
+/// A raster's strip: at most this many bytes of its rows (17.3).
+pub const strip_max = 4 << 20;
+
+/// How many of a `w`-pixel raster's rows a strip holds.
+pub fn stripRows(w: u32) u32 {
+ return @max(1, strip_max / (@as(u32, @max(w, 1)) * 4));
+}
+
+/// The strips of `pl`'s raster over its `src` rows, a bit each.
+fn stripsOver(pl: *const Place) u64 {
+ const per = stripRows(pl.w);
+ const lo = @min(pl.src.y, pl.h) / per;
+ const hi = (@min(pl.src.y +| pl.src.h, pl.h) + per - 1) / per;
+ var bits: u64 = 0;
+ for (lo..@min(hi, 64)) |i| bits |= @as(u64, 1) << @intCast(i);
+ return bits;
+}
+
+/// Strip `i` of `pl`'s raster, a `raster` message's payload: its key, its
+/// size, its first row, and its rows' bytes.
+pub fn encodeStrip(pl: *const Place, i: u32, w: *std.Io.Writer) !void {
+ const y0 = i * stripRows(pl.w);
+ const row = @as(usize, pl.w) * 4;
+ try w.writeInt(u64, pl.key, .little);
+ try w.writeInt(u32, pl.w, .little);
+ try w.writeInt(u32, pl.h, .little);
+ try w.writeInt(u32, y0, .little);
+ try w.writeAll(pl.rgba[y0 * row .. @min(pl.h, y0 + stripRows(pl.w)) * row]);
+}
+
+/// A peer's rasters of the panes of a kind, made from their strips by
+/// name (the key with its pane's serial), kept to `max` bytes by least
+/// recent use but for those a Shadow shows (17.3).
+pub const Blobs = struct {
+ list: std.ArrayList(Blob) = .empty,
+ clock: u64 = 0,
+
+ pub const max = 64 << 20;
+ const Blob = struct { name: u64, w: u32, h: u32, bytes: []u8, have: u64 = 0, used: u64 = 0 };
+
+ pub fn deinit(b: *Blobs, gpa: std.mem.Allocator) void {
+ for (b.list.items) |x| gpa.free(x.bytes);
+ b.list.deinit(gpa);
+ }
+
+ /// A `raster` message's payload (`encodeStrip`'s) into raster `name`,
+ /// made at its first strip; `shown`, the pixels Shadows show now.
+ pub fn strip(b: *Blobs, gpa: std.mem.Allocator, name: u64, payload: []const u8, shown: []const [*]const u8) !void {
+ var r: std.Io.Reader = .fixed(payload);
+ _ = try r.takeInt(u64, .little);
+ const w = try r.takeInt(u32, .little);
+ const h = try r.takeInt(u32, .little);
+ const y0 = try r.takeInt(u32, .little);
+ const rows = r.buffered();
+ const row = @as(usize, w) * 4;
+ if (row == 0 or rows.len % row != 0 or y0 % stripRows(w) != 0 or y0 + rows.len / row > h) return error.BadShadow;
+ const x = for (b.list.items) |*x| {
+ if (x.name == name) break x;
+ } else made: {
+ var held: usize = row * h;
+ for (b.list.items) |x| held += x.bytes.len;
+ while (held > max) {
+ var old: ?usize = null;
+ for (b.list.items, 0..) |x, i| if (std.mem.indexOfScalar([*]const u8, shown, x.bytes.ptr) == null and (old == null or x.used < b.list.items[old.?].used)) {
+ old = i;
+ };
+ const i = old orelse break;
+ held -= b.list.items[i].bytes.len;
+ gpa.free(b.list.swapRemove(i).bytes);
+ }
+ const bytes = try gpa.alloc(u8, row * h);
+ errdefer gpa.free(bytes);
+ try b.list.append(gpa, .{ .name = name, .w = w, .h = h, .bytes = bytes });
+ break :made &b.list.items[b.list.items.len - 1];
+ };
+ if (x.w != w or x.h != h) return error.BadShadow;
+ @memcpy(x.bytes[y0 * row ..][0..rows.len], rows);
+ x.have |= @as(u64, 1) << @intCast(@min(63, y0 / stripRows(w)));
+ b.clock += 1;
+ x.used = b.clock;
+ }
+
+ /// Raster `name`'s pixels when it has every strip `pl` shows.
+ fn shows(b: *Blobs, name: u64, pl: *const Place) ?[]u8 {
+ for (b.list.items) |*x| if (x.name == name and x.w == pl.w and x.h == pl.h and stripsOver(pl) & ~x.have == 0) {
+ b.clock += 1;
+ x.used = b.clock;
+ return x.bytes;
+ };
+ return null;
}
};
/// What the authority last sent one peer of a pane of a kind: its rows'
-/// hashes and its placements' keys.
+/// hashes, its placements, and which strips of each raster it holds.
pub const ShadowSent = struct {
serial: u32 = 0,
cols: u16 = 0,
hashes: std.ArrayList(Hash) = .empty,
- keys: std.ArrayList(u64) = .empty,
- pointer: u8 = 0,
+ places: Hash = 0,
+ keys: std.ArrayList(struct { key: u64, have: u64 }) = .empty,
pub fn deinit(s: *ShadowSent, gpa: std.mem.Allocator) void {
s.hashes.deinit(gpa);
s.keys.deinit(gpa);
s.* = .{};
}
+
+ /// The strips of `pl`'s raster over its `src` the peer lacks (a bit
+ /// each, `encodeStrip`'s `i`), counted sent now.
+ pub fn owed(s: *ShadowSent, gpa: std.mem.Allocator, pl: *const Place) !u64 {
+ const need = stripsOver(pl);
+ for (s.keys.items) |*k| if (k.key == pl.key) {
+ defer k.have |= need;
+ return need & ~k.have;
+ };
+ try s.keys.append(gpa, .{ .key = pl.key, .have = need });
+ return need;
+ }
};
-/// Frame `f` of a pane (the pointer its paint asked for) for a peer
-/// that was last sent `sent`: false, and nothing written, when it holds
-/// this frame already. The message is `budget` bytes at most (the wire's
-/// bound, which a peer's reader takes as the link's end past it): each
-/// placement the peer holds crosses as its key, and a new one's raster
-/// only while it fits; one that does not is left out of the frame (the
-/// peer shows the pane without it) and is not tried again until it moves.
-pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Frame, pointer: u8, budget: usize, w: *std.Io.Writer) !bool {
+/// Frame `f` of a pane (the pointer its paint asked for) for a peer that
+/// was last sent `sent`: false, and nothing written, when it holds this
+/// frame already. Its placements cross by key, their pixels as strips
+/// before it (`ShadowSent.owed`), so a frame is small whatever it shows.
+pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Frame, pointer: u8, w: *std.Io.Writer) !bool {
var hashes: std.ArrayList(Hash) = .empty;
errdefer hashes.deinit(gpa);
for (0..f.rows) |y| try hashes.append(gpa, std.hash.Wyhash.hash(0, std.mem.sliceAsBytes(f.cells[y * f.cols ..][0..f.cols])));
- const places = f.places[0..f.n];
- const rowHeld = struct {
- fn at(s: *const ShadowSent, cols: u16, hs: []const Hash, y: usize) bool {
- return s.cols == cols and y < s.hashes.items.len and s.hashes.items[y] == hs[y];
- }
- }.at;
- // Its size but for the placements: the head, each row's hash and the
- // rows the peer does not hold, the placements' count.
- var size: usize = 6;
- for (0..f.rows) |y| size += 9 + if (rowHeld(sent, f.cols, hashes.items, y)) 0 else @as(usize, f.cols) * 20;
- const place_head = 57;
- var crosses: [64]bool = @splat(false);
- var keys: usize = 0;
- for (places, 0..) |pl, i| {
- const held = std.mem.indexOfScalar(u64, sent.keys.items, pl.key) != null;
- const more = place_head + if (held) 0 else @as(usize, pl.w) * pl.h * 4;
- if (i >= crosses.len or size + more > budget) continue;
- crosses[i] = true;
- size += more;
- keys += 1;
- }
- var same = sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.pointer == pointer and sent.keys.items.len == keys;
- if (same) {
- var k: usize = 0;
- for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) {
- if (pl.key != sent.keys.items[k]) same = false;
- k += 1;
- };
- }
- if (same) {
+ const places = f.places[0..@min(f.n, 64)];
+ var ph = std.hash.Wyhash.init(pointer);
+ for (places) |*pl| inline for (.{ pl.w, pl.h, pl.src, pl.at, pl.dst, pl.key }) |v| ph.update(std.mem.asBytes(&v));
+ const places_hash = ph.final();
+ if (sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.places == places_hash) {
hashes.deinit(gpa);
return false;
}
@@ -1450,7 +1522,7 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr
try w.writeByte(pointer);
for (hashes.items, 0..) |h, y| {
try w.writeInt(u64, h, .little);
- const held = rowHeld(sent, f.cols, hashes.items, y);
+ const held = sent.cols == f.cols and y < sent.hashes.items.len and sent.hashes.items[y] == h;
try w.writeByte(@intFromBool(!held));
if (!held) for (f.cells[y * f.cols ..][0..f.cols]) |*c| {
try w.writeAll(&c.text);
@@ -1461,86 +1533,73 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr
try w.writeByte(c.flags);
};
}
- try w.writeByte(@intCast(keys));
- for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) {
+ try w.writeByte(@intCast(places.len));
+ for (places) |pl| {
try w.writeInt(u32, pl.w, .little);
try w.writeInt(u32, pl.h, .little);
inline for (.{ pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.at.x, pl.at.y, pl.at.w, pl.at.h }) |v| try w.writeInt(u32, v, .little);
inline for (.{ pl.dst.x, pl.dst.y, pl.dst.w, pl.dst.h }) |v| try w.writeInt(u16, v, .little);
try w.writeInt(u64, pl.key, .little);
- const held = std.mem.indexOfScalar(u64, sent.keys.items, pl.key) != null;
- try w.writeByte(@intFromBool(!held));
- if (!held) try w.writeAll(pl.rgba[0 .. @as(usize, pl.w) * pl.h * 4]);
- };
+ }
sent.hashes.deinit(gpa);
sent.hashes = hashes;
sent.cols = f.cols;
- sent.pointer = pointer;
- sent.keys.clearRetainingCapacity();
- for (places, 0..) |pl, i| if (i < crosses.len and crosses[i]) try sent.keys.append(gpa, pl.key);
+ sent.places = places_hash;
+ // The strips of the rasters this frame shows: a key gone is sent whole again.
+ var i: usize = 0;
+ while (i < sent.keys.items.len) {
+ const kept = for (places) |pl| {
+ if (pl.key == sent.keys.items[i].key) break true;
+ } else false;
+ if (kept) i += 1 else _ = sent.keys.swapRemove(i);
+ }
return true;
}
-test "a pane of a kind crosses as its frame, a row or a raster the peer holds as its hash" {
+test "a pane of a kind crosses as its frame, a row the peer holds as its hash, a raster by key after its strips over what it shows" {
const gpa = std.testing.allocator;
var cells: [3 * 2]Cell = @splat(std.mem.zeroes(Cell));
cells[0] = .{ .text = "a".* ++ @as([7]u8, @splat(0)), .fg = 1, .bg = 2, .attrs = 0, .len = 1, .flags = Kind.c.PA_PAINTED };
- var pixels: [4]u8 = .{ 1, 2, 3, 4 };
+ // A raster of three strips (4 MiB a strip at 1,024 px), of which it shows the second.
+ const w: u32 = 1024;
+ const per = stripRows(w);
+ const pixels = try gpa.alloc(u8, @as(usize, w) * per * 3 * 4);
+ defer gpa.free(pixels);
+ @memset(pixels, 5);
var f: Kind.Frame = .{ .cells = &cells, .cols = 3, .rows = 2, .n = 1 };
- f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = &pixels, .w = 1, .h = 1, .key = 9 });
+ f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = pixels.ptr, .w = w, .h = per * 3, .src = .{ .x = 0, .y = per + 1, .w = w, .h = 10 }, .key = 9 });
f.places = &f.buf;
var sent: ShadowSent = .{};
defer sent.deinit(gpa);
+ var blobs: Blobs = .{};
+ defer blobs.deinit(gpa);
const sh = try gpa.create(Shadow);
sh.* = .{};
defer sh.deinit(gpa);
var out: std.Io.Writer.Allocating = .init(gpa);
defer out.deinit();
- try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, 1 << 20, &out.writer));
- try sh.apply(gpa, out.written());
+ const name = std.hash.Wyhash.hash(1, std.mem.asBytes(&f.buf[0].key));
+ try std.testing.expectEqual(@as(u64, 0b010), try sent.owed(gpa, &f.buf[0]));
+ try encodeStrip(&f.buf[0], 1, &out.writer);
+ try std.testing.expect(out.written().len <= strip_max + 20);
+ try blobs.strip(gpa, name, out.written(), &.{});
+ out.clearRetainingCapacity();
+ try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, &out.writer));
+ try std.testing.expectEqual(@as(u64, 0), try sh.apply(gpa, out.written(), 1, &blobs));
try std.testing.expectEqualStrings("a", sh.cells[0].text[0..1]);
- try std.testing.expectEqual(@as(u8, 3), sh.rasters.items[0][2]);
- // The same frame again: nothing to send.
+ try std.testing.expectEqual(@as(u8, 5), sh.places.items[0].rgba[per * w * 4]);
+ // The same frame again: nothing to send, no strip owed.
out.clearRetainingCapacity();
- try std.testing.expect(!try encodeShadow(gpa, &sent, &f, 0, 1 << 20, &out.writer));
- // A cell of the second row moves: the first row and the raster go as hashes.
- cells[4].flags = Kind.c.PA_PAINTED;
- try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, 1 << 20, &out.writer));
- try std.testing.expect(out.written().len < 20 + 2 * 9 + 3 * 20 + 60);
- try sh.apply(gpa, out.written());
- try std.testing.expectEqual(@as(u8, Kind.c.PA_PAINTED), sh.cells[4].flags);
- try std.testing.expectEqual(@as(u8, 3), sh.rasters.items[0][2]);
-}
-
-test "a pane of a kind's raster past the message's bound is left out of its frame, the frame within it" {
- const gpa = std.testing.allocator;
- var cells: [2 * 1]Cell = @splat(std.mem.zeroes(Cell));
- var small: [4]u8 = .{ 1, 2, 3, 4 };
- var big: [4 * 64]u8 = @splat(7);
- var f: Kind.Frame = .{ .cells = &cells, .cols = 2, .rows = 1, .n = 2 };
- f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = &big, .w = 8, .h = 8, .key = 1 });
- f.buf[1] = std.mem.zeroInit(Place, .{ .rgba = &small, .w = 1, .h = 1, .key = 2 });
- f.places = &f.buf;
- var sent: ShadowSent = .{};
- defer sent.deinit(gpa);
- const sh = try gpa.create(Shadow);
- sh.* = .{};
- defer sh.deinit(gpa);
- var out: std.Io.Writer.Allocating = .init(gpa);
- defer out.deinit();
- // Room for the frame and the small raster, not the 256-byte one.
- const budget = 200;
- try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, budget, &out.writer));
- try std.testing.expect(out.written().len <= budget);
- try sh.apply(gpa, out.written());
- try std.testing.expectEqual(@as(usize, 1), sh.places.items.len);
- try std.testing.expectEqual(@as(u64, 2), sh.places.items[0].key);
- // Nothing moved: nothing to send, the big one not tried again.
+ try std.testing.expect(!try encodeShadow(gpa, &sent, &f, 0, &out.writer));
+ try std.testing.expectEqual(@as(u64, 0), try sent.owed(gpa, &f.buf[0]));
+ // Its src moves into the third strip: that one is owed, and a peer
+ // without it does not show the raster and asks for it, once.
+ f.buf[0].src.y = 2 * per + 1;
+ try std.testing.expectEqual(@as(u64, 0b100), try sent.owed(gpa, &f.buf[0]));
out.clearRetainingCapacity();
- try std.testing.expect(!try encodeShadow(gpa, &sent, &f, 0, budget, &out.writer));
- // With room, it crosses, the small one as its key.
- try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, 1 << 20, &out.writer));
- try sh.apply(gpa, out.written());
- try std.testing.expectEqual(@as(usize, 2), sh.places.items.len);
- try std.testing.expectEqual(@as(u8, 7), sh.rasters.items[0][5]);
+ try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, &out.writer));
+ try std.testing.expect(out.written().len < 4 + 1 + 2 * 9 + 60 + 8);
+ try std.testing.expectEqual(name, try sh.apply(gpa, out.written(), 1, &blobs));
+ try std.testing.expectEqual(@as(usize, 0), sh.places.items.len);
+ try std.testing.expectEqual(@as(u64, 0), try sh.apply(gpa, out.written(), 1, &blobs));
}
diff --git a/src/wire.zig b/src/wire.zig
index 65b93bf1..3dd08b29 100644
--- a/src/wire.zig
+++ b/src/wire.zig
@@ -9,6 +9,10 @@ const builtin = @import("builtin");
const pardes = @import("pardes.zig");
const config = @import("pardes_config");
+/// 23: a pane of a kind's frame names its rasters by key, their pixels
+/// crossing before it as strips of 4 MiB at most over the rows it shows
+/// (screen kind 5), and a replica lacking one asks for its pane again
+/// (kind 4); a tick's prefix is on the tape, out of the frame;
/// 22: a PDF's pixel scroll (0x1a) is gone with the core's PDF, the pdf
/// addon's panes scrolled by the wheel as any kind's are;
/// 21: a session's views are `Pardes.views` in its dump and its digest
@@ -31,7 +35,7 @@ const config = @import("pardes_config");
/// part B); 14: the hello names a session and
/// carries the semantics hash, and a ping is answered (phase 5's
/// groundwork); 13: the frames went (phase 3).
-pub const version: u16 = 22;
+pub const version: u16 = 23;
/// The terminal's build and the window's step alike (`semantics`).
const family = switch (config.platform) {