simplify runner and simplify suite

runner.zig:
  - rename shadowed bytes_per_op to declared_bytes
  - inline trivial intermediates into the Result return block

suite.zig:
  - collapse verbose if/else in parse_duration_ns into a compact
    expression-style return
This commit is contained in:
2026-05-23 10:23:51 +03:00
parent cdbdb9b351
commit a4243bfcf6
2 changed files with 15 additions and 28 deletions

View File

@@ -43,7 +43,7 @@ pub fn run_one(name: []const u8, f: BenchFn, env: Env, opts: Options) !Result {
var elapsed_ns: u64 = 0;
var alloc_bytes: u64 = 0;
var alloc_count: u64 = 0;
var bytes_per_op: u64 = 0;
var declared_bytes: u64 = 0;
var is_container = false;
var force_report = false;
@@ -66,7 +66,7 @@ pub fn run_one(name: []const u8, f: BenchFn, env: Env, opts: Options) !Result {
elapsed_ns = if (b.accumulated_ns <= 0) 0 else @intCast(@min(b.accumulated_ns, std.math.maxInt(u64)));
alloc_bytes = env.counter.bytes_allocated;
alloc_count = env.counter.allocs;
bytes_per_op = b.bytes_per_op;
declared_bytes = b.bytes_per_op;
is_container = b.is_container;
force_report = b.force_report_allocs;
@@ -78,26 +78,19 @@ pub fn run_one(name: []const u8, f: BenchFn, env: Env, opts: Options) !Result {
}
const fn_n: f64 = @floatFromInt(n);
const ns_per_op_v: f64 = if (n == 0) 0 else @as(f64, @floatFromInt(elapsed_ns)) / fn_n;
const bytes_alloc_per_op: f64 = if (n == 0) 0 else @as(f64, @floatFromInt(alloc_bytes)) / fn_n;
const allocs_per_op: f64 = if (n == 0) 0 else @as(f64, @floatFromInt(alloc_count)) / fn_n;
const mb_per_sec: ?f64 = if (bytes_per_op == 0 or elapsed_ns == 0)
null
else blk: {
const total_bytes: f64 = @floatFromInt(bytes_per_op *| n);
const elapsed_s: f64 = @as(f64, @floatFromInt(elapsed_ns)) / @as(f64, std.time.ns_per_s);
break :blk (total_bytes / (1024.0 * 1024.0)) / elapsed_s;
};
return .{
.name = name,
.n = n,
.elapsed_ns = elapsed_ns,
.ns_per_op = ns_per_op_v,
.bytes_per_op = bytes_alloc_per_op,
.allocs_per_op = allocs_per_op,
.mb_per_sec = mb_per_sec,
.ns_per_op = if (n == 0) 0 else @as(f64, @floatFromInt(elapsed_ns)) / fn_n,
.bytes_per_op = if (n == 0) 0 else @as(f64, @floatFromInt(alloc_bytes)) / fn_n,
.allocs_per_op = if (n == 0) 0 else @as(f64, @floatFromInt(alloc_count)) / fn_n,
.mb_per_sec = if (declared_bytes == 0 or elapsed_ns == 0) null else blk: {
const total_bytes: f64 = @floatFromInt(declared_bytes *| n);
const elapsed_s: f64 = @as(f64, @floatFromInt(elapsed_ns)) / @as(f64, std.time.ns_per_s);
break :blk (total_bytes / (1024.0 * 1024.0)) / elapsed_s;
},
.force_report_allocs = force_report,
.is_container = is_container,
};

View File

@@ -258,17 +258,11 @@ pub fn parse_duration_ns(s: []const u8) !u64 {
const value: u64 = try std.fmt.parseInt(u64, num_str, 10);
if (unit.len == 0 or std.mem.eql(u8, unit, "s")) {
return value *| std.time.ns_per_s;
} else if (std.mem.eql(u8, unit, "ms")) {
return value *| std.time.ns_per_ms;
} else if (std.mem.eql(u8, unit, "us") or std.mem.eql(u8, unit, "µs")) {
return value *| std.time.ns_per_us;
} else if (std.mem.eql(u8, unit, "ns")) {
return value;
} else {
return error.InvalidDuration;
}
return if (std.mem.eql(u8, unit, "ms")) value *| std.time.ns_per_ms
else if (std.mem.eql(u8, unit, "us") or std.mem.eql(u8, unit, "µs")) value *| std.time.ns_per_us
else if (std.mem.eql(u8, unit, "ns")) value
else if (unit.len == 0 or std.mem.eql(u8, unit, "s")) value *| std.time.ns_per_s
else error.InvalidDuration;
}
test "parse_duration_ns" {