authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2023-06-02 20:08:28+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-06-02 20:08:28+02:00
log879f0b9cee9b409160edf10d8b52f73be2bddb4f
tree4d875ad83f648069cc342aa680eb81d49b9b98f2
parent3faf376b081caa47a98a172fe7c7f63f82b150e4
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Fix std.hash benchmarks (#15917)


4 files changed, 94 insertions(+), 61 deletions(-)

lib/std/crypto/benchmark.zig+12-7
......@@ -34,19 +34,24 @@ const hashes = [_]Crypto{
3434 Crypto{ .ty = crypto.hash.Blake3, .name = "blake3" },
3535};
3636
37const block_size: usize = 8 * 8192;
38
3739pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64 {
38 var h = Hash.init(.{});
40 const blocks_count = bytes / block_size;
41 var block: [block_size]u8 = undefined;
42 random.bytes(&block);
3943
40 var block: [Hash.digest_length]u8 = undefined;
41 random.bytes(block[0..]);
44 var h = Hash.init(.{});
4245
43 var offset: usize = 0;
4446 var timer = try Timer.start();
4547 const start = timer.lap();
46 while (offset < bytes) : (offset += block.len) {
47 h.update(block[0..]);
48 for (0..blocks_count) |_| {
49 h.update(&block);
4850 }
49 mem.doNotOptimizeAway(&h);
51 var final: [Hash.digest_length]u8 = undefined;
52 h.final(&final);
53 std.mem.doNotOptimizeAway(final);
54
5055 const end = timer.read();
5156
5257 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
lib/std/crypto/siphash.zig+5-17
......@@ -47,7 +47,6 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
4747 return struct {
4848 const Self = @This();
4949 const block_length = 64;
50 const digest_length = 64;
5150 const key_length = 16;
5251
5352 v0: u64,
......@@ -56,7 +55,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
5655 v3: u64,
5756 msg_len: u8,
5857
59 pub fn init(key: *const [key_length]u8) Self {
58 fn init(key: *const [key_length]u8) Self {
6059 const k0 = mem.readIntLittle(u64, key[0..8]);
6160 const k1 = mem.readIntLittle(u64, key[8..16]);
6261
......@@ -75,7 +74,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
7574 return d;
7675 }
7776
78 pub fn update(self: *Self, b: []const u8) void {
77 fn update(self: *Self, b: []const u8) void {
7978 std.debug.assert(b.len % 8 == 0);
8079
8180 var off: usize = 0;
......@@ -87,12 +86,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
8786 self.msg_len +%= @truncate(u8, b.len);
8887 }
8988
90 pub fn peek(self: Self) [digest_length]u8 {
91 var copy = self;
92 return copy.finalResult();
93 }
94
95 pub fn final(self: *Self, b: []const u8) T {
89 fn final(self: *Self, b: []const u8) T {
9690 std.debug.assert(b.len < 8);
9791
9892 self.msg_len +%= @truncate(u8, b.len);
......@@ -129,14 +123,8 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
129123 return (@as(u128, b2) << 64) | b1;
130124 }
131125
132 pub fn finalResult(self: *Self) [digest_length]u8 {
133 var result: [digest_length]u8 = undefined;
134 self.final(&result);
135 return result;
136 }
137
138126 fn round(self: *Self, b: [8]u8) void {
139 const m = mem.readIntLittle(u64, b[0..8]);
127 const m = mem.readIntLittle(u64, &b);
140128 self.v3 ^= m;
141129
142130 comptime var i: usize = 0;
......@@ -164,7 +152,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round
164152 d.v2 = math.rotl(u64, d.v2, @as(u64, 32));
165153 }
166154
167 pub fn hash(msg: []const u8, key: *const [key_length]u8) T {
155 fn hash(msg: []const u8, key: *const [key_length]u8) T {
168156 const aligned_len = msg.len - (msg.len % 8);
169157 var c = Self.init(key);
170158 @call(.always_inline, update, .{ &c, msg[0..aligned_len] });
lib/std/hash/benchmark.zig+59-19
......@@ -17,11 +17,22 @@ const Hash = struct {
1717 ty: type,
1818 name: []const u8,
1919 has_iterative_api: bool = true,
20 has_crypto_api: bool = false,
2021 init_u8s: ?[]const u8 = null,
2122 init_u64: ?u64 = null,
2223};
2324
2425const hashes = [_]Hash{
26 Hash{
27 .ty = hash.XxHash64,
28 .name = "xxhash64",
29 .init_u64 = 0,
30 },
31 Hash{
32 .ty = hash.XxHash32,
33 .name = "xxhash32",
34 .init_u64 = 0,
35 },
2536 Hash{
2637 .ty = hash.Wyhash,
2738 .name = "wyhash",
......@@ -68,6 +79,18 @@ const hashes = [_]Hash{
6879 .name = "murmur3-32",
6980 .has_iterative_api = false,
7081 },
82 Hash{
83 .ty = hash.SipHash64(1, 3),
84 .name = "siphash64",
85 .has_crypto_api = true,
86 .init_u8s = &[_]u8{0} ** 16,
87 },
88 Hash{
89 .ty = hash.SipHash128(1, 3),
90 .name = "siphash128",
91 .has_crypto_api = true,
92 .init_u8s = &[_]u8{0} ** 16,
93 },
7194};
7295
7396const Result = struct {
......@@ -76,11 +99,17 @@ const Result = struct {
7699};
77100
78101const block_size: usize = 8 * 8192;
102const alignment: usize = 64;
103
104pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Allocator) !Result {
105 const blocks_count = bytes / block_size;
106 var blocks = try allocator.alloc(u8, block_size + alignment * (blocks_count - 1));
107 defer allocator.free(blocks);
108 random.bytes(blocks);
79109
80pub fn benchmarkHash(comptime H: anytype, bytes: usize) !Result {
81110 var h = blk: {
82111 if (H.init_u8s) |init| {
83 break :blk H.ty.init(init);
112 break :blk H.ty.init(init[0..H.ty.key_length]);
84113 }
85114 if (H.init_u64) |init| {
86115 break :blk H.ty.init(init);
......@@ -88,53 +117,60 @@ pub fn benchmarkHash(comptime H: anytype, bytes: usize) !Result {
88117 break :blk H.ty.init();
89118 };
90119
91 var block: [block_size]u8 = undefined;
92 random.bytes(block[0..]);
93
94 var offset: usize = 0;
95120 var timer = try Timer.start();
96121 const start = timer.lap();
97 while (offset < bytes) : (offset += block.len) {
98 h.update(block[0..]);
122 for (0..blocks_count) |i| {
123 h.update(blocks[i * alignment ..][0..block_size]);
99124 }
125 const final = if (H.has_crypto_api) @truncate(u64, h.finalInt()) else h.final();
126 std.mem.doNotOptimizeAway(final);
127
100128 const end = timer.read();
101129
102130 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
103131 const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
104132
105133 return Result{
106 .hash = h.final(),
134 .hash = final,
107135 .throughput = throughput,
108136 };
109137}
110138
111pub fn benchmarkHashSmallKeys(comptime H: anytype, key_size: usize, bytes: usize) !Result {
139pub fn benchmarkHashSmallKeys(comptime H: anytype, key_size: usize, bytes: usize, allocator: std.mem.Allocator) !Result {
140 var blocks = try allocator.alloc(u8, bytes);
141 defer allocator.free(blocks);
142 random.bytes(blocks);
143
112144 const key_count = bytes / key_size;
113 var block: [block_size]u8 = undefined;
114 random.bytes(block[0..]);
115145
116 var i: usize = 0;
117146 var timer = try Timer.start();
118147 const start = timer.lap();
119148
120149 var sum: u64 = 0;
121 while (i < key_count) : (i += 1) {
122 const small_key = block[0..key_size];
123 sum +%= blk: {
150 for (0..key_count) |i| {
151 const small_key = blocks[i * key_size ..][0..key_size];
152 const final = blk: {
124153 if (H.init_u8s) |init| {
125 break :blk H.ty.hash(init, small_key);
154 if (H.has_crypto_api) {
155 break :blk @truncate(u64, H.ty.toInt(small_key, init[0..H.ty.key_length]));
156 } else {
157 break :blk H.ty.hash(init, small_key);
158 }
126159 }
127160 if (H.init_u64) |init| {
128161 break :blk H.ty.hash(init, small_key);
129162 }
130163 break :blk H.ty.hash(small_key);
131164 };
165 sum +%= final;
132166 }
133167 const end = timer.read();
134168
135169 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
136170 const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
137171
172 std.mem.doNotOptimizeAway(sum);
173
138174 return Result{
139175 .hash = sum,
140176 .throughput = throughput,
......@@ -227,6 +263,10 @@ pub fn main() !void {
227263 }
228264 }
229265
266 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
267 defer std.testing.expect(gpa.deinit() == .ok) catch @panic("leak");
268 const allocator = gpa.allocator();
269
230270 inline for (hashes) |H| {
231271 if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
232272 if (!test_iterative_only or H.has_iterative_api) {
......@@ -236,13 +276,13 @@ pub fn main() !void {
236276 // This allows easier comparison between different implementations.
237277 if (H.has_iterative_api) {
238278 prng.seed(seed);
239 const result = try benchmarkHash(H, count);
279 const result = try benchmarkHash(H, count, allocator);
240280 try stdout.print(" iterative: {:5} MiB/s [{x:0<16}]\n", .{ result.throughput / (1 * MiB), result.hash });
241281 }
242282
243283 if (!test_iterative_only) {
244284 prng.seed(seed);
245 const result_small = try benchmarkHashSmallKeys(H, key_size, count);
285 const result_small = try benchmarkHashSmallKeys(H, key_size, count, allocator);
246286 try stdout.print(" small keys: {:5} MiB/s [{x:0<16}]\n", .{ result_small.throughput / (1 * MiB), result_small.hash });
247287 }
248288 }
lib/std/hash/xxhash.zig+18-18
......@@ -126,8 +126,8 @@ pub const XxHash64 = struct {
126126 return b +% prime_4;
127127 }
128128
129 pub fn hash(input: []const u8) u64 {
130 var hasher = XxHash64.init(0);
129 pub fn hash(seed: u64, input: []const u8) u64 {
130 var hasher = XxHash64.init(seed);
131131 hasher.update(input);
132132 return hasher.final();
133133 }
......@@ -236,8 +236,8 @@ pub const XxHash32 = struct {
236236 return acc;
237237 }
238238
239 pub fn hash(input: []const u8) u32 {
240 var hasher = XxHash32.init(0);
239 pub fn hash(seed: u32, input: []const u8) u32 {
240 var hasher = XxHash32.init(seed);
241241 hasher.update(input);
242242 return hasher.final();
243243 }
......@@ -246,23 +246,23 @@ pub const XxHash32 = struct {
246246test "xxhash64" {
247247 const hash = XxHash64.hash;
248248
249 try expectEqual(hash(""), 0xef46db3751d8e999);
250 try expectEqual(hash("a"), 0xd24ec4f1a98c6e5b);
251 try expectEqual(hash("abc"), 0x44bc2cf5ad770999);
252 try expectEqual(hash("message digest"), 0x066ed728fceeb3be);
253 try expectEqual(hash("abcdefghijklmnopqrstuvwxyz"), 0xcfe1f278fa89835c);
254 try expectEqual(hash("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0xaaa46907d3047814);
255 try expectEqual(hash("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 0xe04a477f19ee145d);
249 try expectEqual(hash(0, ""), 0xef46db3751d8e999);
250 try expectEqual(hash(0, "a"), 0xd24ec4f1a98c6e5b);
251 try expectEqual(hash(0, "abc"), 0x44bc2cf5ad770999);
252 try expectEqual(hash(0, "message digest"), 0x066ed728fceeb3be);
253 try expectEqual(hash(0, "abcdefghijklmnopqrstuvwxyz"), 0xcfe1f278fa89835c);
254 try expectEqual(hash(0, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0xaaa46907d3047814);
255 try expectEqual(hash(0, "12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 0xe04a477f19ee145d);
256256}
257257
258258test "xxhash32" {
259259 const hash = XxHash32.hash;
260260
261 try expectEqual(hash(""), 0x02cc5d05);
262 try expectEqual(hash("a"), 0x550d7456);
263 try expectEqual(hash("abc"), 0x32d153ff);
264 try expectEqual(hash("message digest"), 0x7c948494);
265 try expectEqual(hash("abcdefghijklmnopqrstuvwxyz"), 0x63a14d5f);
266 try expectEqual(hash("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0x9c285e64);
267 try expectEqual(hash("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 0x9c05f475);
261 try expectEqual(hash(0, ""), 0x02cc5d05);
262 try expectEqual(hash(0, "a"), 0x550d7456);
263 try expectEqual(hash(0, "abc"), 0x32d153ff);
264 try expectEqual(hash(0, "message digest"), 0x7c948494);
265 try expectEqual(hash(0, "abcdefghijklmnopqrstuvwxyz"), 0x63a14d5f);
266 try expectEqual(hash(0, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0x9c285e64);
267 try expectEqual(hash(0, "12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 0x9c05f475);
268268}