aboutsummaryrefslogtreecommitdiff
path: root/lib/std/hash
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/hash')
-rw-r--r--lib/std/hash/benchmark.zig37
1 files changed, 31 insertions, 6 deletions
diff --git a/lib/std/hash/benchmark.zig b/lib/std/hash/benchmark.zig
index 84d14a35db..85a782340a 100644
--- a/lib/std/hash/benchmark.zig
+++ b/lib/std/hash/benchmark.zig
@@ -19,8 +19,11 @@ const Hash = struct {
has_iterative_api: bool = true,
has_crypto_api: bool = false,
has_anytype_api: ?[]const comptime_int = null,
+ /// `final` value should be read from this field.
+ has_struct_api: ?[]const u8 = null,
init_u8s: ?[]const u8 = null,
init_u64: ?u64 = null,
+ init_default: bool = false,
};
const hashes = [_]Hash{
@@ -54,6 +57,8 @@ const hashes = [_]Hash{
Hash{
.ty = hash.Adler32,
.name = "adler32",
+ .has_struct_api = "adler",
+ .init_default = true,
},
Hash{
.ty = hash.crc.Crc32,
@@ -112,21 +117,29 @@ pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Alloc
const block_count = bytes / block_size;
- var h = blk: {
+ var h: H.ty = blk: {
if (H.init_u8s) |init| {
- break :blk H.ty.init(init[0..H.ty.key_length]);
+ break :blk .init(init[0..H.ty.key_length]);
}
if (H.init_u64) |init| {
- break :blk H.ty.init(init);
+ break :blk .init(init);
}
- break :blk H.ty.init();
+ if (H.init_default) {
+ break :blk .{};
+ }
+ break :blk .init();
};
var timer = try Timer.start();
for (0..block_count) |i| {
h.update(blocks[i * block_size ..][0..block_size]);
}
- const final = if (H.has_crypto_api) @as(u64, @truncate(h.finalInt())) else h.final();
+ const final = if (H.has_struct_api) |field_name|
+ @field(h, field_name)
+ else if (H.has_crypto_api)
+ @as(u64, @truncate(h.finalInt()))
+ else
+ h.final();
std.mem.doNotOptimizeAway(final);
const elapsed_ns = timer.read();
@@ -340,7 +353,9 @@ fn mode(comptime x: comptime_int) comptime_int {
}
pub fn main() !void {
- const stdout = std.fs.File.stdout().deprecatedWriter();
+ var stdout_buffer: [0x100]u8 = undefined;
+ var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
+ const stdout = &stdout_writer.interface;
var buffer: [1024]u8 = undefined;
var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
@@ -360,6 +375,7 @@ pub fn main() !void {
while (i < args.len) : (i += 1) {
if (std.mem.eql(u8, args[i], "--mode")) {
try stdout.print("{}\n", .{builtin.mode});
+ try stdout.flush();
return;
} else if (std.mem.eql(u8, args[i], "--seed")) {
i += 1;
@@ -397,6 +413,7 @@ pub fn main() !void {
key_size = try std.fmt.parseUnsigned(usize, args[i], 10);
if (key_size.? > block_size) {
try stdout.print("key_size cannot exceed block size of {}\n", .{block_size});
+ try stdout.flush();
std.process.exit(1);
}
} else if (std.mem.eql(u8, args[i], "--iterative-only")) {
@@ -416,6 +433,7 @@ pub fn main() !void {
if (test_iterative_only and test_small_key_only) {
try stdout.print("Cannot use iterative-only and small-key-only together!\n", .{});
+ try stdout.flush();
usage();
std.process.exit(1);
}
@@ -428,6 +446,7 @@ pub fn main() !void {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) hash: {
if (!test_iterative_only or H.has_iterative_api) {
try stdout.print("{s}\n", .{H.name});
+ try stdout.flush();
// Always reseed prior to every call so we are hashing the same buffer contents.
// This allows easier comparison between different implementations.
@@ -435,6 +454,7 @@ pub fn main() !void {
prng.seed(seed);
const result = try benchmarkHash(H, count, allocator);
try stdout.print(" iterative: {:5} MiB/s [{x:0<16}]\n", .{ result.throughput / (1 * MiB), result.hash });
+ try stdout.flush();
}
if (!test_iterative_only) {
@@ -447,6 +467,7 @@ pub fn main() !void {
result_small.throughput / size,
result_small.hash,
});
+ try stdout.flush();
if (!test_arrays) break :hash;
if (H.has_anytype_api) |sizes| {
@@ -464,6 +485,7 @@ pub fn main() !void {
result_ptr.throughput / (1 * MiB),
result_ptr.hash,
});
+ try stdout.flush();
}
}
}
@@ -476,6 +498,7 @@ pub fn main() !void {
result_small.throughput / default_small_key_size,
result_small.hash,
});
+ try stdout.flush();
if (!test_arrays) break :hash;
if (H.has_anytype_api) |sizes| {
@@ -488,6 +511,7 @@ pub fn main() !void {
result.throughput / (1 * MiB),
result.hash,
});
+ try stdout.flush();
}
try stdout.print(" array ptr: \n", .{});
inline for (sizes) |exact_size| {
@@ -498,6 +522,7 @@ pub fn main() !void {
result.throughput / (1 * MiB),
result.hash,
});
+ try stdout.flush();
}
}
}