authorgravatar for jagt@live.comjagt <jagt@live.com> 2022-04-23 10:12:06+08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-24 23:01:06-04:00
log76311aebff0f76dba96f589438957a32c364a011
tree1b0a45966326625c9a8deadc90cb7a00f70db0a0
parent963ac60918b39cafdda3cb99eff4cd9d20edd839

std: fix crypto and hash benchmark


2 files changed, 14 insertions(+), 14 deletions(-)

lib/std/crypto/benchmark.zig+13-13
...@@ -297,32 +297,32 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 {...@@ -297,32 +297,32 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 {
297}297}
298298
299const CryptoPwhash = struct {299const CryptoPwhash = struct {
300 hashFn: @compileError("anytype fields are removed from the language"),300 ty: type,
301 params: @compileError("anytype fields are removed from the language"),301 params: *const anyopaque,
302 name: []const u8,302 name: []const u8,
303};303};
304const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };304const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };
305const pwhashes = [_]CryptoPwhash{305const pwhashes = [_]CryptoPwhash{
306 .{ .hashFn = crypto.pwhash.bcrypt.strHash, .params = bcrypt_params, .name = "bcrypt" },306 .{ .ty = crypto.pwhash.bcrypt, .params = &bcrypt_params, .name = "bcrypt" },
307 .{307 .{
308 .hashFn = crypto.pwhash.scrypt.strHash,308 .ty = crypto.pwhash.scrypt,
309 .params = crypto.pwhash.scrypt.Params.interactive,309 .params = &crypto.pwhash.scrypt.Params.interactive,
310 .name = "scrypt",310 .name = "scrypt",
311 },311 },
312 .{312 .{
313 .hashFn = crypto.pwhash.argon2.strHash,313 .ty = crypto.pwhash.argon2,
314 .params = crypto.pwhash.argon2.Params.interactive_2id,314 .params = &crypto.pwhash.argon2.Params.interactive_2id,
315 .name = "argon2",315 .name = "argon2",
316 },316 },
317};317};
318318
319fn benchmarkPwhash(319fn benchmarkPwhash(
320 comptime hashFn: anytype,320 comptime ty: anytype,
321 comptime params: anytype,321 comptime params: *const anyopaque,
322 comptime count: comptime_int,322 comptime count: comptime_int,
323) !f64 {323) !f64 {
324 const password = "testpass" ** 2;324 const password = "testpass" ** 2;
325 const opts = .{ .allocator = std.testing.allocator, .params = params, .encoding = .phc };325 const opts = .{ .allocator = std.testing.allocator, .params = @ptrCast(*const ty.Params, params).*, .encoding = .phc };
326 var buf: [256]u8 = undefined;326 var buf: [256]u8 = undefined;
327327
328 var timer = try Timer.start();328 var timer = try Timer.start();
...@@ -330,7 +330,7 @@ fn benchmarkPwhash(...@@ -330,7 +330,7 @@ fn benchmarkPwhash(
330 {330 {
331 var i: usize = 0;331 var i: usize = 0;
332 while (i < count) : (i += 1) {332 while (i < count) : (i += 1) {
333 _ = try hashFn(password, opts, &buf);333 _ = try ty.strHash(password, opts, &buf);
334 mem.doNotOptimizeAway(&buf);334 mem.doNotOptimizeAway(&buf);
335 }335 }
336 }336 }
...@@ -463,8 +463,8 @@ pub fn main() !void {...@@ -463,8 +463,8 @@ pub fn main() !void {
463463
464 inline for (pwhashes) |H| {464 inline for (pwhashes) |H| {
465 if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {465 if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
466 const throughput = try benchmarkPwhash(H.hashFn, H.params, mode(64));466 const throughput = try benchmarkPwhash(H.ty, H.params, mode(64));
467 try stdout.print("{s:>17}: {d:.3} s/ops\n", .{ H.name, throughput });467 try stdout.print("{s:>17}: {d:10.3} s/ops\n", .{ H.name, throughput });
468 }468 }
469 }469 }
470}470}
lib/std/hash/benchmark.zig+1-1
...@@ -230,7 +230,7 @@ pub fn main() !void {...@@ -230,7 +230,7 @@ pub fn main() !void {
230 inline for (hashes) |H| {230 inline for (hashes) |H| {
231 if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {231 if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
232 if (!test_iterative_only or H.has_iterative_api) {232 if (!test_iterative_only or H.has_iterative_api) {
233 try stdout.print("{}\n", .{H.name});233 try stdout.print("{s}\n", .{H.name});
234234
235 // Always reseed prior to every call so we are hashing the same buffer contents.235 // Always reseed prior to every call so we are hashing the same buffer contents.
236 // This allows easier comparison between different implementations.236 // This allows easier comparison between different implementations.