summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 02:27:48 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit529cf5121e231f66ce5bc26e5618a4fec2c40913 (patch)
treebeea40395e976fcea154862162e64331809f5808
parent07d1e5066ef38c99cb112cb709fb719e3470bd0d (diff)
downloadpardes-529cf5121e231f66ce5bc26e5618a4fec2c40913.tar.gz
pardes-529cf5121e231f66ce5bc26e5618a4fec2c40913.zip
walk: one byte-dump reader for every root set: Load(roots)'s methods never named the root set but to compare its table with the dump's, yet each set (the session's, a closed pane's, a terminal's Parts, a kind's SlotRecord, Handover's Carried) compiled its own copy of them all (Load(Pane) alone 0.88 MB of Debug code); they are Loader's, a struct of its own, whose init is told this build's table, and Load(roots) is its init with the set's table, so every caller reads as before; a move, the methods' text the same but one level less indented and init's comparison (`jj diff -w` shows it); core-test's forced full recompile (Debug, one filter) 112.95e9 to 110.75e9 instructions (-1.9 % of the base's 120.12e9), the Debug test's text 82.82 MB to 81.48 MB; a session's byte dump, its image and a closed pane's save are the same bytes before and after (but the clock's), each build loads the other's with no note and the same digest, wire.semantics and the tape's shape unchanged; core-test 984 of 984; the changelog says so
-rw-r--r--src/CHANGELOG.md6
-rw-r--r--src/walk.zig992
2 files changed, 504 insertions, 494 deletions
diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md
index c1a78828..2fe5aa1c 100644
--- a/src/CHANGELOG.md
+++ b/src/CHANGELOG.md
@@ -42,10 +42,12 @@
or in a window by the rule above it.
- An edit to the core recompiles it about 1 s sooner again (`core-test`,
- 6 % fewer of the compiler's instructions, 3 MB less code in a Debug
+ 8 % fewer of the compiler's instructions, 4.4 MB less code in a Debug
test): a load's note on a field (another build's dump) is written from
the type's and the field's names at run time, where every type and
- field had its own copy of the code that writes one.
+ field had its own copy of the code that writes one, and one byte
+ dump's reader serves a session, a pane and the rest, where each had its
+ own.
- An edit to the core recompiles it about 3 s sooner (`core-test`, 13 s to
10 s): the build keeps each grammar's highlight patterns that pardes
diff --git a/src/walk.zig b/src/walk.zig
index ffa97af2..e3738bde 100644
--- a/src/walk.zig
+++ b/src/walk.zig
@@ -1006,547 +1006,555 @@ const SrcType = struct {
};
const SrcField = struct { name: []const u8, ty: u32 = 0, value: i128 = 0 };
-/// Reads a dump of `roots`, one `into` or `get` each in order.
+/// Reads a dump of `roots`, one `into` or `get` each in order: a `Loader`
+/// told this build's table of them.
pub fn Load(comptime roots: []const type) type {
return struct {
- const Self = @This();
- const types = graph(roots);
+ pub fn init(gpa: std.mem.Allocator, bytes: []const u8) DecodeError!Loader {
+ return Loader.init(gpa, bytes, tableOf(graph(roots)));
+ }
+ };
+}
- /// Where what is read is made (a `replica.alloc` field's own
- /// allocator while it is read).
- gpa: std.mem.Allocator,
- /// The loader's own lists'.
- own: std.mem.Allocator,
- bytes: []const u8,
- at: usize = 0,
- arena: std.heap.ArenaAllocator,
- src: []SrcType = &.{},
- /// Its table is this build's: no name is asked.
- same: bool = false,
- /// Each pointer and slice made so far: what a reference finds, and
- /// what `rollback` frees.
- shared: std.ArrayList(Made) = .empty,
- /// What a type's `dumpRead` made, freed by its `dumpFree` too.
- customs: std.ArrayList(struct { ptr: *anyopaque, gpa: std.mem.Allocator, free: *const fn (*anyopaque, std.mem.Allocator) void }) = .empty,
- /// What was let go, for the person whose session it is.
- notes: std.ArrayList(u8) = .empty,
- /// The field being read, for a note.
- field: []const u8 = "",
- /// Where a field's annotation (`replica.alloc`) says its values are
- /// made instead of `gpa`.
- allocs: Allocators = .{},
+/// `Load`'s reader, one for every root set: it reads by the dump's own
+/// table, and this build's (`table`) only says whether the two are one.
+pub const Loader = struct {
+ const Self = @This();
- pub fn init(gpa: std.mem.Allocator, bytes: []const u8) DecodeError!Self {
- var l: Self = .{ .gpa = gpa, .own = gpa, .bytes = bytes, .arena = .init(gpa) };
- errdefer l.arena.deinit();
- if (!std.mem.startsWith(u8, bytes, dump_magic)) return error.BadDump;
- l.at = dump_magic.len;
- const table_at = l.at;
- try l.readTable();
- l.same = std.mem.eql(u8, tableOf(types), bytes[table_at..l.at]);
- return l;
- }
+ /// Where what is read is made (a `replica.alloc` field's own
+ /// allocator while it is read).
+ gpa: std.mem.Allocator,
+ /// The loader's own lists'.
+ own: std.mem.Allocator,
+ bytes: []const u8,
+ at: usize = 0,
+ arena: std.heap.ArenaAllocator,
+ src: []SrcType = &.{},
+ /// Its table is this build's: no name is asked.
+ same: bool = false,
+ /// Each pointer and slice made so far: what a reference finds, and
+ /// what `rollback` frees.
+ shared: std.ArrayList(Made) = .empty,
+ /// What a type's `dumpRead` made, freed by its `dumpFree` too.
+ customs: std.ArrayList(struct { ptr: *anyopaque, gpa: std.mem.Allocator, free: *const fn (*anyopaque, std.mem.Allocator) void }) = .empty,
+ /// What was let go, for the person whose session it is.
+ notes: std.ArrayList(u8) = .empty,
+ /// The field being read, for a note.
+ field: []const u8 = "",
+ /// Where a field's annotation (`replica.alloc`) says its values are
+ /// made instead of `gpa`.
+ allocs: Allocators = .{},
- pub fn deinit(l: *Self) void {
- l.shared.deinit(l.own);
- l.customs.deinit(l.own);
- l.notes.deinit(l.own);
- l.arena.deinit();
- }
+ pub fn init(gpa: std.mem.Allocator, bytes: []const u8, table: []const u8) DecodeError!Self {
+ var l: Self = .{ .gpa = gpa, .own = gpa, .bytes = bytes, .arena = .init(gpa) };
+ errdefer l.arena.deinit();
+ if (!std.mem.startsWith(u8, bytes, dump_magic)) return error.BadDump;
+ l.at = dump_magic.len;
+ const table_at = l.at;
+ try l.readTable();
+ l.same = std.mem.eql(u8, table, bytes[table_at..l.at]);
+ return l;
+ }
- /// Everything read so far freed: a load that fails leaves nothing.
- /// What it was read into must not be used after (nor freed).
- pub fn rollback(l: *Self) void {
- for (l.customs.items) |c| c.free(c.ptr, c.gpa);
- for (l.shared.items) |m| if (m.bytes.len > 0) m.gpa.rawFree(m.bytes, m.alignment, @returnAddress());
- l.customs.clearRetainingCapacity();
- l.shared.clearRetainingCapacity();
- }
+ pub fn deinit(l: *Self) void {
+ l.shared.deinit(l.own);
+ l.customs.deinit(l.own);
+ l.notes.deinit(l.own);
+ l.arena.deinit();
+ }
- fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment) DecodeError!usize {
- try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa });
- return l.shared.items.len - 1;
- }
+ /// Everything read so far freed: a load that fails leaves nothing.
+ /// What it was read into must not be used after (nor freed).
+ pub fn rollback(l: *Self) void {
+ for (l.customs.items) |c| c.free(c.ptr, c.gpa);
+ for (l.shared.items) |m| if (m.bytes.len > 0) m.gpa.rawFree(m.bytes, m.alignment, @returnAddress());
+ l.customs.clearRetainingCapacity();
+ l.shared.clearRetainingCapacity();
+ }
- fn take(l: *Self, n: usize) DecodeError![]const u8 {
- if (l.bytes.len - l.at < n) return error.EndOfDump;
- defer l.at += n;
- return l.bytes[l.at..][0..n];
- }
+ fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment) DecodeError!usize {
+ try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa });
+ return l.shared.items.len - 1;
+ }
- fn scalar(l: *Self, comptime T: type) DecodeError!T {
- return std.mem.bytesToValue(T, try l.take(@sizeOf(T)));
- }
+ fn take(l: *Self, n: usize) DecodeError![]const u8 {
+ if (l.bytes.len - l.at < n) return error.EndOfDump;
+ defer l.at += n;
+ return l.bytes[l.at..][0..n];
+ }
- /// A count, as this machine counts (a wasm32 one has 32 bits).
- fn count(l: *Self) DecodeError!usize {
- return countOf(try l.scalar(u64));
- }
+ fn scalar(l: *Self, comptime T: type) DecodeError!T {
+ return std.mem.bytesToValue(T, try l.take(@sizeOf(T)));
+ }
- fn name(l: *Self) DecodeError![]const u8 {
- return l.take(try l.scalar(u16));
- }
+ /// A count, as this machine counts (a wasm32 one has 32 bits).
+ fn count(l: *Self) DecodeError!usize {
+ return countOf(try l.scalar(u64));
+ }
- fn readTable(l: *Self) DecodeError!void {
- const a = l.arena.allocator();
- const n = try l.scalar(u32);
- if (n > l.bytes.len) return error.BadDump;
- l.src = try a.alloc(SrcType, n);
- for (l.src) |*t| {
- const k = std.enums.fromInt(Kind, (try l.take(1))[0]) orelse return error.BadDump;
- t.* = .{ .kind = k };
- switch (k) {
- .int => {
- t.signed = (try l.take(1))[0] != 0;
- t.bits = try l.scalar(u16);
- },
- .float => t.bits = try l.scalar(u16),
- .@"enum" => {
- t.signed = (try l.take(1))[0] != 0;
- t.bits = try l.scalar(u16);
- const list = try a.alloc(SrcField, try l.scalar(u32));
- for (list) |*f| f.* = .{ .name = try l.name(), .value = try l.scalar(i128) };
- t.fields = list;
- },
- .array => {
- t.len = try l.scalar(u64);
- t.child = try l.scalar(u32);
- },
- .optional, .one, .slice, .prefix, .error_union => t.child = try l.scalar(u32),
- .@"struct", .@"union" => {
- const nfields = try l.scalar(u32);
- if (nfields > l.bytes.len) return error.BadDump;
- const list = try a.alloc(SrcField, nfields);
- for (list) |*f| f.* = .{ .name = try l.name(), .ty = try l.scalar(u32) };
- t.fields = list;
- },
- .custom => _ = try l.name(),
- .skip, .void, .bool, .err, .bitset => {},
- }
- }
- for (l.src) |t| {
- if (t.child >= n) return error.BadDump;
- for (t.fields) |f| if (f.ty >= n) return error.BadDump;
+ fn name(l: *Self) DecodeError![]const u8 {
+ return l.take(try l.scalar(u16));
+ }
+
+ fn readTable(l: *Self) DecodeError!void {
+ const a = l.arena.allocator();
+ const n = try l.scalar(u32);
+ if (n > l.bytes.len) return error.BadDump;
+ l.src = try a.alloc(SrcType, n);
+ for (l.src) |*t| {
+ const k = std.enums.fromInt(Kind, (try l.take(1))[0]) orelse return error.BadDump;
+ t.* = .{ .kind = k };
+ switch (k) {
+ .int => {
+ t.signed = (try l.take(1))[0] != 0;
+ t.bits = try l.scalar(u16);
+ },
+ .float => t.bits = try l.scalar(u16),
+ .@"enum" => {
+ t.signed = (try l.take(1))[0] != 0;
+ t.bits = try l.scalar(u16);
+ const list = try a.alloc(SrcField, try l.scalar(u32));
+ for (list) |*f| f.* = .{ .name = try l.name(), .value = try l.scalar(i128) };
+ t.fields = list;
+ },
+ .array => {
+ t.len = try l.scalar(u64);
+ t.child = try l.scalar(u32);
+ },
+ .optional, .one, .slice, .prefix, .error_union => t.child = try l.scalar(u32),
+ .@"struct", .@"union" => {
+ const nfields = try l.scalar(u32);
+ if (nfields > l.bytes.len) return error.BadDump;
+ const list = try a.alloc(SrcField, nfields);
+ for (list) |*f| f.* = .{ .name = try l.name(), .ty = try l.scalar(u32) };
+ t.fields = list;
+ },
+ .custom => _ = try l.name(),
+ .skip, .void, .bool, .err, .bitset => {},
}
}
-
- pub fn note(l: *Self, comptime fmt: []const u8, args: anytype) void {
- if (l.notes.items.len > 4096) return;
- l.notes.print(l.own, fmt ++ "\n", args) catch {};
+ for (l.src) |t| {
+ if (t.child >= n) return error.BadDump;
+ for (t.fields) |f| if (f.ty >= n) return error.BadDump;
}
+ }
- /// A note on a type's field, its names given as run-time strings:
- /// one instance of `note` (and of std's `print`) a message, where
- /// a tuple of the comptime names made one a type, field and message.
- fn noteField(l: *Self, comptime what: []const u8, type_name: []const u8, field: []const u8) void {
- l.note("{s}.{s}: " ++ what, .{ type_name, field });
- }
+ pub fn note(l: *Self, comptime fmt: []const u8, args: anytype) void {
+ if (l.notes.items.len > 4096) return;
+ l.notes.print(l.own, fmt ++ "\n", args) catch {};
+ }
- fn root(l: *Self) DecodeError!u32 {
- const ty = try l.scalar(u32);
- if (ty >= l.src.len) return error.BadDump;
- return ty;
- }
+ /// A note on a type's field, its names given as run-time strings:
+ /// one instance of `note` (and of std's `print`) a message, where
+ /// a tuple of the comptime names made one a type, field and message.
+ fn noteField(l: *Self, comptime what: []const u8, type_name: []const u8, field: []const u8) void {
+ l.note("{s}.{s}: " ++ what, .{ type_name, field });
+ }
- /// The next root, made: what it points at is `gpa`'s.
- pub fn get(l: *Self, comptime T: type) DecodeError!T {
- var out: T = undefined;
- try l.fresh(T, &out);
- try l.value(T, try l.root(), &out);
- return out;
- }
+ fn root(l: *Self) DecodeError!u32 {
+ const ty = try l.scalar(u32);
+ if (ty >= l.src.len) return error.BadDump;
+ return ty;
+ }
- /// The next root read into `out` as it stands: a field the dump
- /// holds is replaced (what it held is not freed), every other one
- /// is left as it is. A field another build cannot give is left too,
- /// and noted.
- pub fn into(l: *Self, comptime T: type, out: *T) DecodeError!void {
- const ty = try l.root();
- const s = l.src[ty];
- if (s.kind != .@"struct") return error.BadDump;
- try l.fields(T, s, out, true);
- }
+ /// The next root, made: what it points at is `gpa`'s.
+ pub fn get(l: *Self, comptime T: type) DecodeError!T {
+ var out: T = undefined;
+ try l.fresh(T, &out);
+ try l.value(T, try l.root(), &out);
+ return out;
+ }
- fn fresh(l: *Self, comptime T: type, out: *T) DecodeError!void {
- freshOf(T, out, l.gpa);
- }
+ /// The next root read into `out` as it stands: a field the dump
+ /// holds is replaced (what it held is not freed), every other one
+ /// is left as it is. A field another build cannot give is left too,
+ /// and noted.
+ pub fn into(l: *Self, comptime T: type, out: *T) DecodeError!void {
+ const ty = try l.root();
+ const s = l.src[ty];
+ if (s.kind != .@"struct") return error.BadDump;
+ try l.fields(T, s, out, true);
+ }
- /// Whether the source's `ty` and `T` are of one kind, else the
- /// source's value passed over.
- fn kindMatches(l: *Self, comptime T: type, ty: u32) DecodeError!bool {
- if (l.same) return true;
- const want = comptime kindOf(Canon(T));
- const got = l.src[ty].kind;
- if (got == want) return true;
- // A field that came to hold a count and so many, or stopped.
- if ((want == .array and got == .prefix) or (want == .prefix and got == .array)) return true;
- try l.skip(ty);
- return false;
- }
+ fn fresh(l: *Self, comptime T: type, out: *T) DecodeError!void {
+ freshOf(T, out, l.gpa);
+ }
- fn readInt(l: *Self, comptime I: type, s: SrcType) DecodeError!I {
- if (l.same) {
- const info = @typeInfo(I).int;
- const W = @Int(info.signedness, intBytes(info.bits) * 8);
- return @intCast(try l.scalar(W));
- }
- const n = intBytes(s.bits);
- const raw = try l.take(n);
- var wide: u128 = 0;
- for (raw, 0..) |b, i| wide |= @as(u128, b) << @intCast(i * 8);
- if (s.signed and n > 0) {
- const shift: u7 = @intCast(128 - n * 8);
- const signed: i128 = @as(i128, @bitCast(wide << shift)) >> shift;
- return std.math.cast(I, signed) orelse error.Incompatible;
- }
- return std.math.cast(I, wide) orelse error.Incompatible;
+ /// Whether the source's `ty` and `T` are of one kind, else the
+ /// source's value passed over.
+ fn kindMatches(l: *Self, comptime T: type, ty: u32) DecodeError!bool {
+ if (l.same) return true;
+ const want = comptime kindOf(Canon(T));
+ const got = l.src[ty].kind;
+ if (got == want) return true;
+ // A field that came to hold a count and so many, or stopped.
+ if ((want == .array and got == .prefix) or (want == .prefix and got == .array)) return true;
+ try l.skip(ty);
+ return false;
+ }
+
+ fn readInt(l: *Self, comptime I: type, s: SrcType) DecodeError!I {
+ if (l.same) {
+ const info = @typeInfo(I).int;
+ const W = @Int(info.signedness, intBytes(info.bits) * 8);
+ return @intCast(try l.scalar(W));
+ }
+ const n = intBytes(s.bits);
+ const raw = try l.take(n);
+ var wide: u128 = 0;
+ for (raw, 0..) |b, i| wide |= @as(u128, b) << @intCast(i * 8);
+ if (s.signed and n > 0) {
+ const shift: u7 = @intCast(128 - n * 8);
+ const signed: i128 = @as(i128, @bitCast(wide << shift)) >> shift;
+ return std.math.cast(I, signed) orelse error.Incompatible;
}
+ return std.math.cast(I, wide) orelse error.Incompatible;
+ }
- /// `T` read from the source's type `ty` into `out`, which `fresh`
- /// has made ready.
- fn value(l: *Self, comptime T: type, ty: u32, out: *T) DecodeError!void {
- if (comptime isAtomic(T)) {
- const R = @FieldType(T, "raw");
- var raw: R = undefined;
- try l.fresh(R, &raw);
- try l.value(R, ty, &raw);
- out.* = .{ .raw = raw };
- return;
- }
- // A field this build compiles out (a void field): passed over,
- // said only when it held something.
- if (comptime kindOf(Canon(T)) == .void) if (!l.same and l.src[ty].kind != .void) {
- const held = l.src[ty].kind != .optional or (l.at < l.bytes.len and l.bytes[l.at] != 0);
- try l.skip(ty);
- if (held) l.note("{s}: not in this build", .{l.field});
- return;
- };
- if (!try l.kindMatches(T, ty)) return error.Incompatible;
- const s = l.src[ty];
- if (comptime isList(T)) {
- const E = @typeInfo(@FieldType(T, "items")).pointer.child;
- const got = try l.slice(E, s);
- out.* = .empty;
- out.items = got;
- out.capacity = got.len;
- return;
- }
- switch (comptime kindOf(T)) {
- .skip, .void => {},
- .bool => out.* = (try l.take(1))[0] != 0,
- .int => out.* = try l.readInt(T, s),
- .float => out.* = if (l.same or s.bits == @typeInfo(T).float.bits) try l.scalar(T) else return error.Incompatible,
- .@"enum" => {
- const e = @typeInfo(T).@"enum";
- if (l.same) {
- out.* = @fromBackingInt(@intCast(try l.readInt(e.tag_type, s)));
- return;
- }
- const n = try l.readInt(i128, s);
- const was = for (s.fields) |f| {
- if (f.value == n) break f.name;
- } else null;
- if (was) |w| inline for (e.field_names) |field| if (std.mem.eql(u8, w, field)) {
- out.* = @field(T, field);
- return;
- };
- if (e.mode == .nonexhaustive and was == null)
- if (std.math.cast(e.tag_type, n)) |tag| {
- out.* = @fromBackingInt(@intCast(tag));
- return;
- };
- l.note("{s}: no {s} here", .{ l.field, was orelse "such value" });
- return error.Incompatible;
- },
- .optional => {
- const C = @typeInfo(T).optional.child;
- if ((try l.take(1))[0] == 0) {
- out.* = null;
- return;
- }
- if (comptime kindOf(Canon(C)) == .skip) {
- out.* = if (comptime C == std.mem.Allocator) l.gpa else null;
- return;
- }
- // A slice's or a pointer's null is its address: set to
- // an undefined payload, the optional itself is undefined,
- // and ReleaseSafe's optimizer read it as null (a pane's
- // `?[]u8` command one byte long, every field after it out
- // of step). Read through a local and set it from that.
- if (comptime @typeInfo(C) == .pointer) {
- var payload: C = undefined;
- try l.value(C, s.child, &payload);
- out.* = payload;
- return;
- }
- out.* = @as(C, undefined);
- try l.fresh(C, &out.*.?);
- l.value(C, s.child, &out.*.?) catch |err| {
- out.* = null;
- return err;
- };
- },
- .one => {
- const C = @typeInfo(T).pointer.child;
- if (try l.ref()) |again| {
- out.* = @ptrCast(@alignCast(again.ptr));
+ /// `T` read from the source's type `ty` into `out`, which `fresh`
+ /// has made ready.
+ fn value(l: *Self, comptime T: type, ty: u32, out: *T) DecodeError!void {
+ if (comptime isAtomic(T)) {
+ const R = @FieldType(T, "raw");
+ var raw: R = undefined;
+ try l.fresh(R, &raw);
+ try l.value(R, ty, &raw);
+ out.* = .{ .raw = raw };
+ return;
+ }
+ // A field this build compiles out (a void field): passed over,
+ // said only when it held something.
+ if (comptime kindOf(Canon(T)) == .void) if (!l.same and l.src[ty].kind != .void) {
+ const held = l.src[ty].kind != .optional or (l.at < l.bytes.len and l.bytes[l.at] != 0);
+ try l.skip(ty);
+ if (held) l.note("{s}: not in this build", .{l.field});
+ return;
+ };
+ if (!try l.kindMatches(T, ty)) return error.Incompatible;
+ const s = l.src[ty];
+ if (comptime isList(T)) {
+ const E = @typeInfo(@FieldType(T, "items")).pointer.child;
+ const got = try l.slice(E, s);
+ out.* = .empty;
+ out.items = got;
+ out.capacity = got.len;
+ return;
+ }
+ switch (comptime kindOf(T)) {
+ .skip, .void => {},
+ .bool => out.* = (try l.take(1))[0] != 0,
+ .int => out.* = try l.readInt(T, s),
+ .float => out.* = if (l.same or s.bits == @typeInfo(T).float.bits) try l.scalar(T) else return error.Incompatible,
+ .@"enum" => {
+ const e = @typeInfo(T).@"enum";
+ if (l.same) {
+ out.* = @fromBackingInt(@intCast(try l.readInt(e.tag_type, s)));
+ return;
+ }
+ const n = try l.readInt(i128, s);
+ const was = for (s.fields) |f| {
+ if (f.value == n) break f.name;
+ } else null;
+ if (was) |w| inline for (e.field_names) |field| if (std.mem.eql(u8, w, field)) {
+ out.* = @field(T, field);
+ return;
+ };
+ if (e.mode == .nonexhaustive and was == null)
+ if (std.math.cast(e.tag_type, n)) |tag| {
+ out.* = @fromBackingInt(@intCast(tag));
return;
- }
- const one = try l.gpa.create(C);
- const at = l.made(std.mem.asBytes(one), .of(C)) catch |err| {
- l.gpa.destroy(one);
- return err;
};
- try l.fresh(C, one);
- l.value(C, s.child, one) catch |err| {
- // Left half made: no later reference may find it.
- l.shared.items[at].dead = true;
- return err;
- };
- out.* = one;
- },
- .slice => {
- const E = @typeInfo(T).pointer.child;
- const got = try l.slice(E, s);
- if (comptime @typeInfo(T).pointer.sentinel_ptr != null) @compileError("walk: a sentinel slice: " ++ @typeName(T));
- out.* = got;
- },
- .array => {
- const E = @typeInfo(T).array.child;
- const n: u64 = if (s.kind == .prefix) try l.scalar(u64) else s.len;
- try l.elements(E, s.child, n, out);
- },
- .prefix => unreachable,
- .@"struct" => try l.fields(T, s, out, false),
- .@"union" => {
- const u = @typeInfo(T).@"union";
- const ordinal = try l.scalar(u16);
- if (ordinal >= s.fields.len) return error.BadDump;
- const was = s.fields[ordinal];
- inline for (u.field_names, u.field_types, 0..) |field, F, i| {
- if (if (l.same) ordinal == i else std.mem.eql(u8, was.name, field)) {
- out.* = @unionInit(T, field, undefined);
- try l.fresh(F, &@field(out.*, field));
- return l.value(F, was.ty, &@field(out.*, field));
- }
+ l.note("{s}: no {s} here", .{ l.field, was orelse "such value" });
+ return error.Incompatible;
+ },
+ .optional => {
+ const C = @typeInfo(T).optional.child;
+ if ((try l.take(1))[0] == 0) {
+ out.* = null;
+ return;
+ }
+ if (comptime kindOf(Canon(C)) == .skip) {
+ out.* = if (comptime C == std.mem.Allocator) l.gpa else null;
+ return;
+ }
+ // A slice's or a pointer's null is its address: set to
+ // an undefined payload, the optional itself is undefined,
+ // and ReleaseSafe's optimizer read it as null (a pane's
+ // `?[]u8` command one byte long, every field after it out
+ // of step). Read through a local and set it from that.
+ if (comptime @typeInfo(C) == .pointer) {
+ var payload: C = undefined;
+ try l.value(C, s.child, &payload);
+ out.* = payload;
+ return;
+ }
+ out.* = @as(C, undefined);
+ try l.fresh(C, &out.*.?);
+ l.value(C, s.child, &out.*.?) catch |err| {
+ out.* = null;
+ return err;
+ };
+ },
+ .one => {
+ const C = @typeInfo(T).pointer.child;
+ if (try l.ref()) |again| {
+ out.* = @ptrCast(@alignCast(again.ptr));
+ return;
+ }
+ const one = try l.gpa.create(C);
+ const at = l.made(std.mem.asBytes(one), .of(C)) catch |err| {
+ l.gpa.destroy(one);
+ return err;
+ };
+ try l.fresh(C, one);
+ l.value(C, s.child, one) catch |err| {
+ // Left half made: no later reference may find it.
+ l.shared.items[at].dead = true;
+ return err;
+ };
+ out.* = one;
+ },
+ .slice => {
+ const E = @typeInfo(T).pointer.child;
+ const got = try l.slice(E, s);
+ if (comptime @typeInfo(T).pointer.sentinel_ptr != null) @compileError("walk: a sentinel slice: " ++ @typeName(T));
+ out.* = got;
+ },
+ .array => {
+ const E = @typeInfo(T).array.child;
+ const n: u64 = if (s.kind == .prefix) try l.scalar(u64) else s.len;
+ try l.elements(E, s.child, n, out);
+ },
+ .prefix => unreachable,
+ .@"struct" => try l.fields(T, s, out, false),
+ .@"union" => {
+ const u = @typeInfo(T).@"union";
+ const ordinal = try l.scalar(u16);
+ if (ordinal >= s.fields.len) return error.BadDump;
+ const was = s.fields[ordinal];
+ inline for (u.field_names, u.field_types, 0..) |field, F, i| {
+ if (if (l.same) ordinal == i else std.mem.eql(u8, was.name, field)) {
+ out.* = @unionInit(T, field, undefined);
+ try l.fresh(F, &@field(out.*, field));
+ return l.value(F, was.ty, &@field(out.*, field));
}
- try l.skip(was.ty);
- l.note("{s}: no {s} here", .{ l.field, was.name });
+ }
+ try l.skip(was.ty);
+ l.note("{s}: no {s} here", .{ l.field, was.name });
+ return error.Incompatible;
+ },
+ .err, .error_union => unreachable,
+ .custom => {
+ const bytes = try l.take(try l.count());
+ T.dumpRead(out, l.gpa, bytes) catch |err| {
+ if (err == error.OutOfMemory) return error.OutOfMemory;
+ l.note("{s}: {t}", .{ l.field, err });
return error.Incompatible;
- },
- .err, .error_union => unreachable,
- .custom => {
- const bytes = try l.take(try l.count());
- T.dumpRead(out, l.gpa, bytes) catch |err| {
- if (err == error.OutOfMemory) return error.OutOfMemory;
- l.note("{s}: {t}", .{ l.field, err });
- return error.Incompatible;
- };
- if (comptime @hasDecl(T, "dumpFree")) try l.customs.append(l.own, .{ .ptr = out, .gpa = l.gpa, .free = @ptrCast(&T.dumpFree) });
- },
- .bitset => {
- const bits = try l.count();
- out.* = try .initEmpty(l.gpa, bits);
- const words = (bits + @bitSizeOf(usize) - 1) / @bitSizeOf(usize);
- if (words > 0) @memcpy(std.mem.sliceAsBytes(out.masks[0..words]), try l.take(words * @sizeOf(usize)));
- },
- }
+ };
+ if (comptime @hasDecl(T, "dumpFree")) try l.customs.append(l.own, .{ .ptr = out, .gpa = l.gpa, .free = @ptrCast(&T.dumpFree) });
+ },
+ .bitset => {
+ const bits = try l.count();
+ out.* = try .initEmpty(l.gpa, bits);
+ const words = (bits + @bitSizeOf(usize) - 1) / @bitSizeOf(usize);
+ if (words > 0) @memcpy(std.mem.sliceAsBytes(out.masks[0..words]), try l.take(words * @sizeOf(usize)));
+ },
}
+ }
- /// A reference to a pointer or slice made before, or null for a
- /// new one (its length then read).
- fn ref(l: *Self) DecodeError!?[]u8 {
- const n = try l.scalar(u32);
- if (n == 0) return null;
- if (n > l.shared.items.len) return error.BadDump;
- const got = l.shared.items[n - 1];
- if (got.dead) return error.Incompatible;
- return got.bytes;
- }
+ /// A reference to a pointer or slice made before, or null for a
+ /// new one (its length then read).
+ fn ref(l: *Self) DecodeError!?[]u8 {
+ const n = try l.scalar(u32);
+ if (n == 0) return null;
+ if (n > l.shared.items.len) return error.BadDump;
+ const got = l.shared.items[n - 1];
+ if (got.dead) return error.Incompatible;
+ return got.bytes;
+ }
- fn slice(l: *Self, comptime E: type, s: SrcType) DecodeError![]E {
- if (try l.ref()) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))];
- const n = try l.count();
- if (n == 0) return &.{};
- if (n > l.bytes.len) return error.BadDump;
- const many = try l.gpa.alloc(E, n);
- const at = l.made(std.mem.sliceAsBytes(many), .of(E)) catch |err| {
- l.gpa.free(many);
- return err;
- };
- l.elements(E, s.child, n, many) catch |err| {
- l.shared.items[at].dead = true;
- return err;
+ fn slice(l: *Self, comptime E: type, s: SrcType) DecodeError![]E {
+ if (try l.ref()) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))];
+ const n = try l.count();
+ if (n == 0) return &.{};
+ if (n > l.bytes.len) return error.BadDump;
+ const many = try l.gpa.alloc(E, n);
+ const at = l.made(std.mem.sliceAsBytes(many), .of(E)) catch |err| {
+ l.gpa.free(many);
+ return err;
+ };
+ l.elements(E, s.child, n, many) catch |err| {
+ l.shared.items[at].dead = true;
+ return err;
+ };
+ return many;
+ }
+
+ /// `n` elements of the source's type `ty` into `out` (an array or
+ /// a slice), as many as it holds, the rest passed over.
+ fn elements(l: *Self, comptime E: type, ty: u32, n: u64, out: []E) DecodeError!void {
+ const keep: usize = @intCast(@min(n, out.len));
+ const s = l.src[ty];
+ // Bytes copied whole between builds when both sides agree on
+ // them: integers and bools (an enum's numbers may not be the other's).
+ const bulk = if (comptime flat(E)) l.same or (comptime flatHash(E)) and s.kind == (comptime kindOf(E)) and
+ (s.kind == .bool or (s.bits == @bitSizeOf(E) and s.signed == (@typeInfo(E) == .int and @typeInfo(E).int.signedness == .signed))) else false;
+ if (bulk) {
+ if (n > l.bytes.len) return error.EndOfDump;
+ @memcpy(std.mem.sliceAsBytes(out[0..keep]), try l.take(keep * @sizeOf(E)));
+ _ = try l.take(@intCast((n - keep) * @sizeOf(E)));
+ return;
+ }
+ // An array of optional pointers (a session's panes): one that
+ // cannot be read is left out, not the array.
+ const droppable = comptime @typeInfo(E) == .optional and @typeInfo(@typeInfo(E).optional.child) == .pointer;
+ var failed = false;
+ for (0..try countOf(n)) |i| {
+ if (i >= keep) {
+ try l.skip(ty);
+ continue;
+ }
+ try l.fresh(E, &out[i]);
+ l.value(E, ty, &out[i]) catch |err| {
+ if (err != error.Incompatible) return err;
+ if (droppable) {
+ out[i] = null;
+ l.note("{s}[{d}] left out", .{ l.field, i });
+ } else failed = true;
};
- return many;
}
+ if (failed) return error.Incompatible;
+ }
- /// `n` elements of the source's type `ty` into `out` (an array or
- /// a slice), as many as it holds, the rest passed over.
- fn elements(l: *Self, comptime E: type, ty: u32, n: u64, out: []E) DecodeError!void {
- const keep: usize = @intCast(@min(n, out.len));
- const s = l.src[ty];
- // Bytes copied whole between builds when both sides agree on
- // them: integers and bools (an enum's numbers may not be the other's).
- const bulk = if (comptime flat(E)) l.same or (comptime flatHash(E)) and s.kind == (comptime kindOf(E)) and
- (s.kind == .bool or (s.bits == @bitSizeOf(E) and s.signed == (@typeInfo(E) == .int and @typeInfo(E).int.signedness == .signed))) else false;
- if (bulk) {
- if (n > l.bytes.len) return error.EndOfDump;
- @memcpy(std.mem.sliceAsBytes(out[0..keep]), try l.take(keep * @sizeOf(E)));
- _ = try l.take(@intCast((n - keep) * @sizeOf(E)));
- return;
+ fn fields(l: *Self, comptime T: type, s: SrcType, out: *T, existing: bool) DecodeError!void {
+ const names = comptime dumpFields(T);
+ const packed_struct = comptime @typeInfo(T).@"struct".layout == .@"packed";
+ const outer = l.field;
+ defer l.field = outer;
+ if (l.same) {
+ if (s.fields.len != names.len) return error.BadDump;
+ inline for (names, 0..) |field, k| {
+ l.field = field;
+ try l.oneField(T, field, s.fields[k].ty, out, packed_struct, existing);
}
- // An array of optional pointers (a session's panes): one that
- // cannot be read is left out, not the array.
- const droppable = comptime @typeInfo(E) == .optional and @typeInfo(@typeInfo(E).optional.child) == .pointer;
- var failed = false;
- for (0..try countOf(n)) |i| {
- if (i >= keep) {
- try l.skip(ty);
- continue;
- }
- try l.fresh(E, &out[i]);
- l.value(E, ty, &out[i]) catch |err| {
+ return;
+ }
+ var found: std.StaticBitSet(@max(1, names.len)) = .empty;
+ var failed = false;
+ for (s.fields) |f| {
+ const k: ?usize = inline for (names, 0..) |field, k| {
+ if (std.mem.eql(u8, f.name, field)) break k;
+ } else null;
+ const at = k orelse {
+ try l.skip(f.ty);
+ continue;
+ };
+ found.set(at);
+ inline for (names, 0..) |field, i| if (i == at) {
+ l.field = field;
+ l.oneField(T, field, f.ty, out, packed_struct, existing) catch |err| {
if (err != error.Incompatible) return err;
- if (droppable) {
- out[i] = null;
- l.note("{s}[{d}] left out", .{ l.field, i });
+ const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?];
+ if (comptime attr.default_value_ptr) |dp| {
+ @field(out, field) = @as(*const @FieldType(T, field), @ptrCast(@alignCast(dp))).*;
+ l.noteField("its kind changed; its default taken", @typeName(T), field);
+ } else if (existing) {
+ l.noteField("its kind changed; left as it was", @typeName(T), field);
} else failed = true;
};
- }
- if (failed) return error.Incompatible;
+ };
}
-
- fn fields(l: *Self, comptime T: type, s: SrcType, out: *T, existing: bool) DecodeError!void {
- const names = comptime dumpFields(T);
- const packed_struct = comptime @typeInfo(T).@"struct".layout == .@"packed";
- const outer = l.field;
- defer l.field = outer;
- if (l.same) {
- if (s.fields.len != names.len) return error.BadDump;
- inline for (names, 0..) |field, k| {
- l.field = field;
- try l.oneField(T, field, s.fields[k].ty, out, packed_struct, existing);
+ // A field the writer did not have: its default (`fresh`), or
+ // the value cannot be made.
+ inline for (names, 0..) |field, i| if (!found.isSet(i)) {
+ const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?];
+ if (comptime attr.default_value_ptr == null) {
+ if (!existing) {
+ l.noteField("not in the dump", @typeName(T), field);
+ failed = true;
}
- return;
}
- var found: std.StaticBitSet(@max(1, names.len)) = .empty;
- var failed = false;
- for (s.fields) |f| {
- const k: ?usize = inline for (names, 0..) |field, k| {
- if (std.mem.eql(u8, f.name, field)) break k;
- } else null;
- const at = k orelse {
- try l.skip(f.ty);
- continue;
- };
- found.set(at);
- inline for (names, 0..) |field, i| if (i == at) {
- l.field = field;
- l.oneField(T, field, f.ty, out, packed_struct, existing) catch |err| {
- if (err != error.Incompatible) return err;
- const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?];
- if (comptime attr.default_value_ptr) |dp| {
- @field(out, field) = @as(*const @FieldType(T, field), @ptrCast(@alignCast(dp))).*;
- l.noteField("its kind changed; its default taken", @typeName(T), field);
- } else if (existing) {
- l.noteField("its kind changed; left as it was", @typeName(T), field);
- } else failed = true;
- };
- };
- }
- // A field the writer did not have: its default (`fresh`), or
- // the value cannot be made.
- inline for (names, 0..) |field, i| if (!found.isSet(i)) {
- const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?];
- if (comptime attr.default_value_ptr == null) {
- if (!existing) {
- l.noteField("not in the dump", @typeName(T), field);
- failed = true;
- }
- }
- };
- if (failed) return error.Incompatible;
- }
+ };
+ if (failed) return error.Incompatible;
+ }
- fn oneField(l: *Self, comptime T: type, comptime field: []const u8, ty: u32, out: *T, comptime packed_struct: bool, existing: bool) DecodeError!void {
- const F = @FieldType(T, field);
- const gpa = l.gpa;
- defer l.gpa = gpa;
- if (comptime allocOf(T, field)) |which| if (@field(l.allocs, which)) |a| {
- l.gpa = a;
- };
- if (comptime cutArray(T, field)) {
- return l.value(F, ty, &@field(out, field));
- }
- // A struct held in place in one read as it stands (the root's):
- // what it does not hold stays as it is too.
- if (comptime kindOf(Canon(F)) == .@"struct" and Canon(F) == F and @typeInfo(F).@"struct".layout != .@"packed") if (existing) {
- if (!try l.kindMatches(F, ty)) return error.Incompatible;
- return l.fields(F, l.src[ty], &@field(out, field), true);
- };
- if (packed_struct) {
- var copy: F = undefined;
- try l.fresh(F, &copy);
- try l.value(F, ty, &copy);
- @field(out, field) = copy;
- return;
- }
- try l.fresh(F, &@field(out, field));
- try l.value(F, ty, &@field(out, field));
+ fn oneField(l: *Self, comptime T: type, comptime field: []const u8, ty: u32, out: *T, comptime packed_struct: bool, existing: bool) DecodeError!void {
+ const F = @FieldType(T, field);
+ const gpa = l.gpa;
+ defer l.gpa = gpa;
+ if (comptime allocOf(T, field)) |which| if (@field(l.allocs, which)) |a| {
+ l.gpa = a;
+ };
+ if (comptime cutArray(T, field)) {
+ return l.value(F, ty, &@field(out, field));
}
+ // A struct held in place in one read as it stands (the root's):
+ // what it does not hold stays as it is too.
+ if (comptime kindOf(Canon(F)) == .@"struct" and Canon(F) == F and @typeInfo(F).@"struct".layout != .@"packed") if (existing) {
+ if (!try l.kindMatches(F, ty)) return error.Incompatible;
+ return l.fields(F, l.src[ty], &@field(out, field), true);
+ };
+ if (packed_struct) {
+ var copy: F = undefined;
+ try l.fresh(F, &copy);
+ try l.value(F, ty, &copy);
+ @field(out, field) = copy;
+ return;
+ }
+ try l.fresh(F, &@field(out, field));
+ try l.value(F, ty, &@field(out, field));
+ }
- /// The source's value of type `ty` passed over.
- fn skip(l: *Self, ty: u32) DecodeError!void {
- const s = l.src[ty];
- switch (s.kind) {
- .skip, .void => {},
- .bool => _ = try l.take(1),
- .int, .@"enum" => _ = try l.take(intBytes(s.bits)),
- .float => _ = try l.take(s.bits / 8),
- .optional => if ((try l.take(1))[0] != 0) try l.skip(s.child),
- .one => if (try l.scalar(u32) == 0) {
+ /// The source's value of type `ty` passed over.
+ fn skip(l: *Self, ty: u32) DecodeError!void {
+ const s = l.src[ty];
+ switch (s.kind) {
+ .skip, .void => {},
+ .bool => _ = try l.take(1),
+ .int, .@"enum" => _ = try l.take(intBytes(s.bits)),
+ .float => _ = try l.take(s.bits / 8),
+ .optional => if ((try l.take(1))[0] != 0) try l.skip(s.child),
+ .one => if (try l.scalar(u32) == 0) {
+ _ = try l.made(&.{}, .@"1");
+ l.shared.items[l.shared.items.len - 1].dead = true;
+ try l.skip(s.child);
+ },
+ .slice => if (try l.scalar(u32) == 0) {
+ const n = try l.scalar(u64);
+ if (n > 0) {
_ = try l.made(&.{}, .@"1");
l.shared.items[l.shared.items.len - 1].dead = true;
- try l.skip(s.child);
- },
- .slice => if (try l.scalar(u32) == 0) {
- const n = try l.scalar(u64);
- if (n > 0) {
- _ = try l.made(&.{}, .@"1");
- l.shared.items[l.shared.items.len - 1].dead = true;
- }
- try l.skipMany(s.child, n);
- },
- .prefix => try l.skipMany(s.child, try l.scalar(u64)),
- .array => try l.skipMany(s.child, s.len),
- .@"struct" => for (s.fields) |f| try l.skip(f.ty),
- .@"union" => {
- const ordinal = try l.scalar(u16);
- if (ordinal >= s.fields.len) return error.BadDump;
- try l.skip(s.fields[ordinal].ty);
- },
- .err => _ = try l.name(),
- .error_union => if ((try l.take(1))[0] != 0) try l.skip(s.child) else {
- _ = try l.name();
- },
- .custom => _ = try l.take(try l.count()),
- .bitset => _ = try l.take((try l.count() + 63) / 64 * 8),
- }
+ }
+ try l.skipMany(s.child, n);
+ },
+ .prefix => try l.skipMany(s.child, try l.scalar(u64)),
+ .array => try l.skipMany(s.child, s.len),
+ .@"struct" => for (s.fields) |f| try l.skip(f.ty),
+ .@"union" => {
+ const ordinal = try l.scalar(u16);
+ if (ordinal >= s.fields.len) return error.BadDump;
+ try l.skip(s.fields[ordinal].ty);
+ },
+ .err => _ = try l.name(),
+ .error_union => if ((try l.take(1))[0] != 0) try l.skip(s.child) else {
+ _ = try l.name();
+ },
+ .custom => _ = try l.take(try l.count()),
+ .bitset => _ = try l.take((try l.count() + 63) / 64 * 8),
}
+ }
- fn skipMany(l: *Self, ty: u32, n: u64) DecodeError!void {
- const s = l.src[ty];
- if (s.kind == .int or s.kind == .bool) {
- const size: u64 = if (s.kind == .bool) 1 else intBytes(s.bits);
- if (n > l.bytes.len) return error.EndOfDump;
- _ = try l.take(@intCast(n * size));
- return;
- }
- for (0..try countOf(n)) |_| try l.skip(ty);
+ fn skipMany(l: *Self, ty: u32, n: u64) DecodeError!void {
+ const s = l.src[ty];
+ if (s.kind == .int or s.kind == .bool) {
+ const size: u64 = if (s.kind == .bool) 1 else intBytes(s.bits);
+ if (n > l.bytes.len) return error.EndOfDump;
+ _ = try l.take(@intCast(n * size));
+ return;
}
- };
-}
+ for (0..try countOf(n)) |_| try l.skip(ty);
+ }
+};
/// `out` as a new `T` is before a load reads its fields: each field's
/// default, an allocator `gpa`.