-
Notifications
You must be signed in to change notification settings - Fork 5
fix: add compatibility for Zig 0.16 ArrayList and timer changes #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
05bcfac
b1ba240
6f5e9e1
aeef8d7
c9e8990
de41d46
06a49e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,45 @@ const pack = msgpack.Pack( | |
| bufferType.read, | ||
| ); | ||
|
|
||
| const is_zig_16 = builtin.zig_version.minor >= 16; | ||
| const BenchRuntime = if (is_zig_16) struct { | ||
| var io: ?std.Io = null; | ||
| } else struct {}; | ||
|
|
||
| /// Get monotonic time in nanoseconds on Zig 0.16+. | ||
| fn getTimeNs() u64 { | ||
| if (is_zig_16) { | ||
| const io = BenchRuntime.io orelse @panic("benchmark runtime io is not initialized"); | ||
| return @intCast(std.Io.Clock.awake.now(io).nanoseconds); | ||
| } | ||
|
|
||
| unreachable; | ||
| } | ||
|
|
||
| /// Benchmark timer helper | ||
| /// Run a benchmark and print results | ||
| fn benchmark( | ||
| comptime name: []const u8, | ||
| comptime iterations: usize, | ||
| comptime func: fn (allocator: std.mem.Allocator) anyerror!void, | ||
| ) !void { | ||
| var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| defer _ = gpa.deinit(); | ||
| const allocator = gpa.allocator(); | ||
| const allocator = std.heap.smp_allocator; | ||
|
|
||
| // Warmup | ||
| for (0..10) |_| { | ||
| try func(allocator); | ||
| } | ||
|
|
||
| // Actual benchmark | ||
| var timer = try std.time.Timer.start(); | ||
| var legacy_timer: if (is_zig_16) void else std.time.Timer = if (is_zig_16) {} else undefined; | ||
| if (!is_zig_16) legacy_timer = try std.time.Timer.start(); | ||
| const start_ns = if (is_zig_16) getTimeNs() else 0; | ||
|
Comment on lines
+49
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The conditional logic for handling the timer across Zig versions is quite clunky. Instead of manually implementing |
||
|
|
||
| for (0..iterations) |_| { | ||
| try func(allocator); | ||
| } | ||
| const elapsed_ns = timer.read(); | ||
|
|
||
| const elapsed_ns = if (is_zig_16) getTimeNs() - start_ns else legacy_timer.read(); | ||
|
|
||
| const avg_ns = elapsed_ns / iterations; | ||
| const ops_per_sec = if (avg_ns > 0) (1_000_000_000 / avg_ns) else 0; | ||
|
|
@@ -850,7 +867,7 @@ fn benchMixedTypesRead(allocator: std.mem.Allocator) !void { | |
| // Main Benchmark Runner | ||
| // ============================================================================ | ||
|
|
||
| pub fn main() !void { | ||
| fn runBenchmarks() !void { | ||
| std.debug.print("\n", .{}); | ||
| std.debug.print("=" ** 80 ++ "\n", .{}); | ||
| std.debug.print("MessagePack Benchmark Suite\n", .{}); | ||
|
|
@@ -936,3 +953,16 @@ pub fn main() !void { | |
| std.debug.print("Benchmark Complete\n", .{}); | ||
| std.debug.print("=" ** 80 ++ "\n", .{}); | ||
| } | ||
|
|
||
| const BenchEntry = if (is_zig_16) struct { | ||
| pub fn main(init: std.process.Init) !void { | ||
| BenchRuntime.io = init.io; | ||
| try runBenchmarks(); | ||
| } | ||
| } else struct { | ||
| pub fn main() !void { | ||
| try runBenchmarks(); | ||
| } | ||
| }; | ||
|
|
||
| pub const main = BenchEntry.main; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The manual implementation of
getTimeNsintroduces significant boilerplate and potential portability issues (e.g., dependency on libc forclock_gettimeon non-Windows platforms). For Zig 0.16, the recommended replacement forstd.time.Timerisstd.time.Instant, which provides a cross-platform monotonic clock API. Additionally, the Windows implementation lacks a check for a zero frequency fromRtlQueryPerformanceFrequency, which would cause a division by zero.