summaryrefslogtreecommitdiff
path: root/src/walk.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/walk.zig')
-rw-r--r--src/walk.zig28
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;
}