From 1c729e8781679803d0641fd777dc47299b165400 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 5 Nov 2026 00:00:52 -0300 Subject: walk: hash names isAtomic and isBitset where it spelled them out again, plainBytes is flatHash and isSigned is written at its one use; a dump's float of another width is incompatible, as any other changed kind, not converted; unlockBlobs is the store it wrapped, Dump.shared was memo.count(), elements takes the slice it is always given, and five names nothing outside reads lose pub; two doc lines go back to lengthOf Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/walk.zig | 68 +++++++++++++++++++----------------------------------------- 1 file changed, 21 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/walk.zig b/src/walk.zig index facc4bd9..601b1387 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -74,29 +74,25 @@ fn lockBlobs() void { while (blob_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint(); } -fn unlockBlobs() void { - blob_lock.store(false, .release); -} - /// The blob at `addr` is freed: its word, if it has one, goes with it. pub fn forget(addr: usize) void { if (blob_count.load(.acquire) == 0) return; lockBlobs(); - defer unlockBlobs(); + defer blob_lock.store(false, .release); if (blob_words.remove(addr)) blob_count.store(blob_words.count(), .release); } /// The word kept for the blob at `addr` (a test's). pub fn wordOf(addr: usize) ?u64 { lockBlobs(); - defer unlockBlobs(); + defer blob_lock.store(false, .release); return if (blob_words.get(addr)) |b| b.word else null; } /// Every word forgotten (the bench's cold digests). pub fn forgetAll() void { lockBlobs(); - defer unlockBlobs(); + defer blob_lock.store(false, .release); blob_words.clearRetainingCapacity(); blob_count.store(0, .release); } @@ -107,7 +103,7 @@ fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { const word: u64 = word: { const kept: ?u64 = if (blob_words_on) kept: { lockBlobs(); - defer unlockBlobs(); + defer blob_lock.store(false, .release); const b = blob_words.get(addr) orelse break :kept null; break :kept if (b.len == items.len) b.word else null; } else null; @@ -130,7 +126,7 @@ fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { if (kept) |k| if (k != w) std.debug.panic("walk: the {d}-byte blob at 0x{x} is not what its kept word was: written in place, or freed without memory.freeRaw", .{ items.len, addr }); if (blob_words_on and kept == null) { lockBlobs(); - defer unlockBlobs(); + defer blob_lock.store(false, .release); blob_words.put(std.heap.page_allocator, addr, .{ .len = items.len, .word = w }) catch {}; blob_count.store(blob_words.count(), .release); } @@ -175,7 +171,7 @@ pub fn makeWords(texts: []const []const u8) void { one.update(items); const w = one.final(); lockBlobs(); - defer unlockBlobs(); + defer blob_lock.store(false, .release); const got = blob_words.getOrPut(std.heap.page_allocator, @intFromPtr(items.ptr)) catch continue; if (!got.found_existing) got.value_ptr.* = .{ .len = items.len, .word = w }; blob_count.store(blob_words.count(), .release); @@ -250,7 +246,7 @@ fn opaqueTo(comptime F: type) bool { }; } -pub fn excluded(comptime T: type, comptime name: []const u8) bool { +fn excluded(comptime T: type, comptime name: []const u8) bool { @setEvalBranchQuota(100_000); if (!@hasDecl(T, "replica")) return false; inline for (.{ "local", "derived", "authority" }) |kind| { @@ -261,7 +257,6 @@ pub fn excluded(comptime T: type, comptime name: []const u8) bool { return false; } -/// How far the field `len` names says its array is written (null: none). /// What a `len`-cut field holds: a fixed array's, or a slice's room (a /// selection's ranges, made once and used `nsel` deep). fn Element(comptime F: type) type { @@ -277,6 +272,7 @@ pub fn lengthIn(v: anytype, comptime len: []const u8) usize { return if (@typeInfo(@TypeOf(n)) == .optional) (if (n) |k| k else 0) else n; } +/// How far the field `len` names says its array is written (null: none). pub fn lengthOf(comptime T: type, comptime name: []const u8) ?[]const u8 { @setEvalBranchQuota(100_000); if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), "len")) return null; @@ -320,11 +316,11 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void { if (comptime isList(T)) return hashSlice(@typeInfo(@FieldType(T, "items")).pointer.child, v.items, h); // std.atomic.Value: what it holds. - if (comptime @hasField(T, "raw") and s.field_names.len == 1 and @hasDecl(T, "load")) return hash(@FieldType(T, "raw"), &v.raw, h); + if (comptime isAtomic(T)) return hash(@FieldType(T, "raw"), &v.raw, h); // A worker's future is a handle of the host's, never state. if (comptime @hasField(T, "any_future")) return; // std.DynamicBitSetUnmanaged: its bits. - if (comptime @hasField(T, "bit_length") and @hasField(T, "masks") and s.field_names.len == 2) { + if (comptime isBitset(T)) { h.update(std.mem.asBytes(&v.bit_length)); return h.update(std.mem.sliceAsBytes(v.masks[0 .. (v.bit_length + @bitSizeOf(usize) - 1) / @bitSizeOf(usize)])); } @@ -547,7 +543,7 @@ pub const Reader = struct { /// What an error read back is when this build has none of that name (an /// OS's own, a newer build's): the tape is another build's, and diverges. -pub const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError }; +const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError }; /// The inverse of `encode`; slices (and what a pointer points at) are made /// in `a`. A field `encode` left out is its default. @@ -722,7 +718,7 @@ test "an annotated field is in no digest, and a length cuts its array" { // // Native byte order: the machines pardes runs on are all little-endian. -pub const dump_magic = "pardes-dump\x00\x02\n"; +const dump_magic = "pardes-dump\x00\x02\n"; const Kind = enum(u8) { skip, void, bool, int, float, @"enum", optional, one, slice, array, prefix, @"struct", @"union", err, error_union, custom, bitset }; @@ -954,20 +950,6 @@ fn putInt(comptime I: type, n: I, w: *std.Io.Writer) !void { try w.writeAll(std.mem.asBytes(&wide)); } -/// Bytes the walk may copy whole between builds when both sides agree on -/// them: integers and bools (an enum's numbers may not be the other's). -fn isSigned(comptime T: type) bool { - return @typeInfo(T) == .int and @typeInfo(T).int.signedness == .signed; -} - -fn plainBytes(comptime T: type) bool { - return switch (@typeInfo(T)) { - .int => @sizeOf(T) * 8 == @bitSizeOf(T), - .bool => true, - else => false, - }; -} - /// 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 { @@ -1001,7 +983,6 @@ pub fn Dump(comptime roots: []const type) type { gpa: std.mem.Allocator, out: std.Io.Writer.Allocating, memo: std.AutoHashMapUnmanaged(Key, u32) = .empty, - shared: u32 = 0, /// Set: a `.blob`'s bytes (of `blob_min` and up) are not written but /// noted, where they go and what they are; the dump with them put /// back in place is the dump. A replica's join streams them from @@ -1050,8 +1031,7 @@ pub fn Dump(comptime roots: []const type) type { try writeU32(w, got.value_ptr.*); return true; } - d.shared += 1; - got.value_ptr.* = d.shared; + got.value_ptr.* = @intCast(d.memo.count()); try writeU32(w, 0); return false; } @@ -1160,12 +1140,12 @@ pub fn save(comptime T: type, gpa: std.mem.Allocator, v: *const T) ![]u8 { return d.finish(); } -pub const DecodeError = error{ EndOfDump, BadDump, Incompatible, OutOfMemory }; +const DecodeError = error{ EndOfDump, BadDump, Incompatible, OutOfMemory }; /// The allocators a `replica.alloc` annotation may name, beside the /// loader's own: `.alloc = .{ .{ "image", "image" } }` makes what field /// `image` holds with `allocs.image`. -pub const Allocators = struct { +const Allocators = struct { image: ?std.mem.Allocator = null, pdf: ?std.mem.Allocator = null, terminal: ?std.mem.Allocator = null, @@ -1438,15 +1418,7 @@ pub fn Load(comptime roots: []const type) type { .skip, .void => {}, .bool => out.* = (try l.take(1))[0] != 0, .int => out.* = try l.readInt(T, s), - .float => { - if (l.same or s.bits == @typeInfo(T).float.bits) { - out.* = try l.scalar(T); - } else if (s.bits == 32) { - out.* = @floatCast(try l.scalar(f32)); - } else if (s.bits == 64) { - out.* = @floatCast(try l.scalar(f64)); - } else return error.Incompatible; - }, + .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) { @@ -1594,11 +1566,13 @@ pub fn Load(comptime roots: []const type) type { /// `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: anytype) DecodeError!void { + 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]; - const bulk = if (comptime flat(E)) l.same or (comptime plainBytes(E)) and s.kind == (comptime kindOf(E)) and - (s.kind == .bool or (s.bits == @bitSizeOf(E) and s.signed == isSigned(E))) else false; + // 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))); -- cgit v1.3