From 07d1e5066ef38c99cb112cb709fb719e3470bd0d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 02:22:35 -0300 Subject: walk: a load's note on a field is written from the names at run time: Load's and Get's three notes on a field (its kind changed, its default taken or left as it was; not in the dump) passed .{ @typeName(T), field }, a tuple of comptime strings, so `note` and std's print under it were compiled once a type, field and message (2,506 instances of note, 2,225 of Writer.print); noteField takes the two names as run-time strings and is one instance a message; the notes' text is the same, byte for byte; core-test's forced full recompile (Debug, one filter) 120.12e9 to 112.95e9 instructions (-6.0 %), generic instances 19,383 to 14,799, the Debug test's text 85.84 MB to 82.82 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; the changelog says so --- src/walk.zig | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/walk.zig') diff --git a/src/walk.zig b/src/walk.zig index 3641d327..ffa97af2 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -1135,6 +1135,13 @@ pub fn Load(comptime roots: []const type) type { l.notes.print(l.own, fmt ++ "\n", args) catch {}; } + /// 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 }); + } + fn root(l: *Self) DecodeError!u32 { const ty = try l.scalar(u32); if (ty >= l.src.len) return error.BadDump; @@ -1441,9 +1448,9 @@ pub fn Load(comptime roots: []const type) type { 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.note("{s}.{s}: its kind changed; its default taken", .{ @typeName(T), field }); + l.noteField("its kind changed; its default taken", @typeName(T), field); } else if (existing) { - l.note("{s}.{s}: its kind changed; left as it was", .{ @typeName(T), field }); + l.noteField("its kind changed; left as it was", @typeName(T), field); } else failed = true; }; }; @@ -1454,7 +1461,7 @@ pub fn Load(comptime roots: []const type) type { const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?]; if (comptime attr.default_value_ptr == null) { if (!existing) { - l.note("{s}.{s}: not in the dump", .{ @typeName(T), field }); + l.noteField("not in the dump", @typeName(T), field); failed = true; } } @@ -1854,6 +1861,11 @@ pub const Get = struct { l.notes.print(l.own, fmt ++ "\n", args) catch {}; } + /// A note on a type's field, its names given at run time (Load's). + fn noteField(l: *Get, comptime what: []const u8, type_name: []const u8, field: []const u8) void { + l.note("{s}.{s}: " ++ what, .{ type_name, field }); + } + /// The value at `c`, made: what it points at is `gpa`'s. pub fn get(l: *Get, comptime T: type, c: Cursor) Error!T { var out: T = undefined; @@ -2270,9 +2282,9 @@ pub const Get = struct { 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.note("{s}.{s}: its kind changed; its default taken", .{ @typeName(T), field }); + l.noteField("its kind changed; its default taken", @typeName(T), field); } else if (existing) { - l.note("{s}.{s}: its kind changed; left as it was", .{ @typeName(T), field }); + l.noteField("its kind changed; left as it was", @typeName(T), field); } else failed = true; }; }; @@ -2284,7 +2296,7 @@ pub const Get = struct { const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?]; if (comptime attr.default_value_ptr == null and kindOf(Canon(F)) != .void) { if (!existing) { - l.note("{s}.{s}: not in the dump", .{ @typeName(T), field }); + l.noteField("not in the dump", @typeName(T), field); failed = true; } } else if (comptime @typeInfo(F) == .optional and defaultIsNull(T, field)) if (existing) { -- cgit v1.3 From 529cf5121e231f66ce5bc26e5618a4fec2c40913 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 02:27:48 -0300 Subject: 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 --- src/CHANGELOG.md | 6 +- src/walk.zig | 994 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 505 insertions(+), 495 deletions(-) (limited to 'src/walk.zig') 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); - - /// 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 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; + pub fn init(gpa: std.mem.Allocator, bytes: []const u8) DecodeError!Loader { + return Loader.init(gpa, bytes, tableOf(graph(roots))); } + }; +} - pub fn deinit(l: *Self) void { - l.shared.deinit(l.own); - l.customs.deinit(l.own); - l.notes.deinit(l.own); - l.arena.deinit(); - } +/// `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(); - /// 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(); - } + /// 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 = .{}, - 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; - } + 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; + } - 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]; - } + 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 scalar(l: *Self, comptime T: type) DecodeError!T { - return std.mem.bytesToValue(T, try l.take(@sizeOf(T))); - } + /// 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(); + } - /// 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 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 name(l: *Self) DecodeError![]const u8 { - return l.take(try l.scalar(u16)); - } + 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 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 scalar(l: *Self, comptime T: type) DecodeError!T { + return std.mem.bytesToValue(T, try l.take(@sizeOf(T))); + } + + /// 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 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, ©); - try l.value(F, ty, ©); - @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, ©); + try l.value(F, ty, ©); + @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`. -- cgit v1.3 From a27c2cb3846a6e2db28250600d762d19c1006eaf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 02:38:44 -0300 Subject: walk: the tape's shape writes its numbers with decimal, not comptimePrint: shapeOf wrote each integer's and float's width, each enum value and each array's length with std.fmt.comptimePrint, an inline function and so never memoized, about 5e6 of the compiler's instructions a call and some 600 calls (354 integers, 220 enum values) in every compile of the core; decimal writes `n` as {d} does, memoized, in five lines; the shape is the same 10,960 bytes and wire.semantics the same 05b2cba8834b1d7c; core-test's forced full recompile (Debug, one filter) 104.95e9 to 103.41e9 instructions (-1.3 % of the base's 120.12e9), the Debug test's text unchanged; a session's byte dump, its image and a closed pane's save the same bytes (but the clock's) and each build loads the other's; test: decimal against {d} for 0 to maxInt(u128) and minInt(i128); core-test 985 of 985; the changelog says so --- src/CHANGELOG.md | 5 +++-- src/walk.zig | 22 ++++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'src/walk.zig') diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 1bece4b2..ce98a862 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -42,13 +42,14 @@ or in a window by the rule above it. - An edit to the core recompiles it about 1.5 s sooner again (`core-test`, - 13 % fewer of the compiler's instructions, 4.4 MB less code in a Debug + 14 % 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; one byte dump's reader serves a session, a pane and the rest, where each had its own; and what tells a replica's build from another's (`wire.semantics`) - joins its two long parts on, where it formatted them, at every compile. + joins its two long parts on, and writes the tape's numbers itself, + where it formatted them, at every compile. - 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 e3738bde..8747937d 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -378,15 +378,15 @@ fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { return switch (@typeInfo(T)) { .void, .null, .undefined => "v", .bool => "b", - .int => |i| std.fmt.comptimePrint("{t}{d}", .{ i.signedness, i.bits }), - .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}), + .int => |i| @tagName(i.signedness) ++ decimal(i.bits), + .float => |f| "f" ++ decimal(f.bits), .@"enum" => |e| blk: { var out: []const u8 = shapeOf(e.tag_type, depth + 1) ++ "{"; - for (e.field_names, e.field_values) |n, v| out = out ++ std.fmt.comptimePrint("{s}={d},", .{ n, v }); + for (e.field_names, e.field_values) |n, v| out = out ++ n ++ "=" ++ decimal(v) ++ ","; break :blk out ++ "}"; }, .optional => |o| if (machinery(o.child)) "0" else "?" ++ shapeOf(o.child, depth + 1), - .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ shapeOf(a.child, depth + 1), + .array => |a| "[" ++ decimal(a.len) ++ "]" ++ shapeOf(a.child, depth + 1), .pointer => |p| switch (p.size) { .one => shapeOf(p.child, depth + 1), .slice => "[]" ++ shapeOf(p.child, depth + 1), @@ -413,6 +413,15 @@ fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { } } +/// `n` in decimal, as `{d}` writes it: memoized, where comptimePrint (an +/// inline function) formats again at every call, 5e6 of the compiler's +/// instructions each. +fn decimal(comptime n: comptime_int) []const u8 { + if (n < 0) return "-" ++ decimal(-n); + if (n < 10) return &.{'0' + n}; + return decimal(n / 10) ++ &[_]u8{'0' + n % 10}; +} + /// An error by its name. An `anyerror` must be one `decode` can read back /// (`errorNamed`): a Debug build stops at one that is not, as it is taped. fn encodeError(comptime E: type, err: E, w: *std.Io.Writer) std.Io.Writer.Error!void { @@ -2584,3 +2593,8 @@ test "a blob of 4 KiB and up is digested by its length, what it holds told by it small[1] = 'x'; try std.testing.expect(digest(File, &f) != was); } + +test "a shape's numbers are written as {d} writes them" { + inline for (.{ 0, 7, 10, 65535, -1, -300, std.math.minInt(i128), std.math.maxInt(u128) }) |n| + try std.testing.expectEqualStrings(std.fmt.comptimePrint("{d}", .{n}), comptime decimal(n)); +} -- cgit v1.3 From 2536110e142a3cb548a43235767ac7ae6efa357c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 02:43:58 -0300 Subject: walk: the tape's shape appends each piece whole: `out = out ++ name ++ ":" ++ shapeOf(F) ++ ","` is ((out ++ name) ++ ":") ++ ..., so the shape so far (up to 11 KB, every union arm and struct field of the tape's types) was copied at comptime once a piece, four times a field, and a comptime copy is costly (32 KB built of 4,000 appends: 226e9 of the compiler's instructions); the piece is put together first and appended once, in shape (each type's line), an enum's values, a struct's and a union's fields; the shape is the same 10,960 bytes and wire.semantics the same 05b2cba8834b1d7c; core-test's forced full recompile (Debug, one filter) 103.41e9 to 101.37e9 instructions (-1.7 % of the base's 120.12e9; all five changes together -15.6 %, 15.5-16.1 s of CPU to 13.6-14.4 s, and 9.0-9.2 s wall against 10.2-11.0 s, both noisy), the Debug test's text unchanged; a session's byte dump, its image and a closed pane's save the same bytes (but the clock's) and each build loads the other's; the changelog says so --- src/CHANGELOG.md | 9 +++++---- src/walk.zig | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'src/walk.zig') diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index ce98a862..b12fc2c5 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -41,15 +41,16 @@ A column resizes by its box, left of its tag; a pane by its own box, or in a window by the rule above it. -- An edit to the core recompiles it about 1.5 s sooner again (`core-test`, - 14 % fewer of the compiler's instructions, 4.4 MB less code in a Debug +- An edit to the core recompiles it about 2 s sooner again (`core-test`, + 16 % 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; one byte dump's reader serves a session, a pane and the rest, where each had its own; and what tells a replica's build from another's (`wire.semantics`) - joins its two long parts on, and writes the tape's numbers itself, - where it formatted them, at every compile. + joins its two long parts on, and the tape's shape writes its numbers + itself and copies each line it builds once, where it formatted them + and copied the line again for each piece, at every compile. - 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 8747937d..0f181cfa 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -367,7 +367,7 @@ pub fn shape(comptime types: []const type) []const u8 { comptime { @setEvalBranchQuota(10_000_000); var out: []const u8 = ""; - for (types) |T| out = out ++ shapeOf(T, 0) ++ "\n"; + for (types) |T| out = out ++ (shapeOf(T, 0) ++ "\n"); return out; } } @@ -382,7 +382,7 @@ fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { .float => |f| "f" ++ decimal(f.bits), .@"enum" => |e| blk: { var out: []const u8 = shapeOf(e.tag_type, depth + 1) ++ "{"; - for (e.field_names, e.field_values) |n, v| out = out ++ n ++ "=" ++ decimal(v) ++ ","; + for (e.field_names, e.field_values) |n, v| out = out ++ (n ++ "=" ++ decimal(v) ++ ","); break :blk out ++ "}"; }, .optional => |o| if (machinery(o.child)) "0" else "?" ++ shapeOf(o.child, depth + 1), @@ -397,13 +397,13 @@ fn shapeOf(comptime T: type, comptime depth: u8) []const u8 { var out: []const u8 = if (s.layout == .@"packed") "p{" else "{"; for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { if (attr.@"comptime" or excluded(T, name)) continue; - out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ","; + out = out ++ (name ++ ":" ++ shapeOf(F, depth + 1) ++ ","); } break :blk out ++ "}"; }, .@"union" => |u| blk: { var out: []const u8 = shapeOf(u.tag_type.?, depth + 1) ++ "("; - for (u.field_names, u.field_types) |name, F| out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ","; + for (u.field_names, u.field_types) |name, F| out = out ++ (name ++ ":" ++ shapeOf(F, depth + 1) ++ ","); break :blk out ++ ")"; }, .error_set => "error", -- cgit v1.3