diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 19:03:47 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 21:13:33 -0300 |
| commit | e4cfd57bc41c5740e3a823a8f05d70d3e87b68ca (patch) | |
| tree | 5237acc412695185258d3b96d7a579663e659158 | |
| parent | ad1a8f4bc4c363e7c6e7a89e14fdab62cb914a82 (diff) | |
| download | pardes-e4cfd57bc41c5740e3a823a8f05d70d3e87b68ca.tar.gz pardes-e4cfd57bc41c5740e3a823a8f05d70d3e87b68ca.zip | |
walk.zig: a dump's type table is made once a process, not for every store record and dump
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| -rw-r--r-- | src/walk.zig | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/walk.zig b/src/walk.zig index 00868133..328557a7 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -649,6 +649,27 @@ fn plainBytes(comptime T: type) bool { }; } +/// The table of `types` as bytes, made once a process (it is the build's): +/// a store record or a dump writes it, a load compares it. +fn tableOf(comptime types: []const type) []const u8 { + const Once = struct { + // Of these types alone: a container that named none of its + // function's parameters would be one for every table. + const of = types; + /// 0 none, 1 being made, 2 made. + var state: std.atomic.Value(u8) = .init(0); + var bytes: []const u8 = &.{}; + }; + if (Once.state.load(.acquire) == 2) return Once.bytes; + if (Once.state.cmpxchgStrong(0, 1, .acquire, .monotonic) == null) { + var out: std.Io.Writer.Allocating = .init(std.heap.page_allocator); + writeTable(types, &out.writer) catch @panic("walk: out of memory for a dump's table"); + Once.bytes = out.toOwnedSlice() catch @panic("walk: out of memory for a dump's table"); + Once.state.store(2, .release); + } else while (Once.state.load(.acquire) != 2) std.atomic.spinLoopHint(); + return Once.bytes; +} + /// Writes a dump of `roots`' values, one `put` each in order. pub fn Dump(comptime roots: []const type) type { return struct { @@ -666,7 +687,7 @@ pub fn Dump(comptime roots: []const type) type { var d: Self = .{ .gpa = gpa, .out = .init(gpa) }; errdefer d.out.deinit(); try d.out.writer.writeAll(dump_magic); - try writeTable(types, &d.out.writer); + try d.out.writer.writeAll(tableOf(types)); return d; } @@ -875,10 +896,7 @@ pub fn Load(comptime roots: []const type) type { l.at = dump_magic.len; const table_at = l.at; try l.readTable(); - var mine: std.Io.Writer.Allocating = .init(gpa); - defer mine.deinit(); - writeTable(types, &mine.writer) catch return error.OutOfMemory; - l.same = std.mem.eql(u8, mine.written(), bytes[table_at..l.at]); + l.same = std.mem.eql(u8, tableOf(types), bytes[table_at..l.at]); return l; } |
