diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 02:38:44 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | a27c2cb3846a6e2db28250600d762d19c1006eaf (patch) | |
| tree | 01a7989f5e3c6039b471d0fb30d20efc2d8a1dce /src/walk.zig | |
| parent | 1a14192c65180397d8a2a3df898817768d371a94 (diff) | |
| download | pardes-a27c2cb3846a6e2db28250600d762d19c1006eaf.tar.gz pardes-a27c2cb3846a6e2db28250600d762d19c1006eaf.zip | |
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
Diffstat (limited to 'src/walk.zig')
| -rw-r--r-- | src/walk.zig | 22 |
1 files changed, 18 insertions, 4 deletions
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)); +} |
