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 {
297297}
298298
299299const CryptoPwhash = struct {
300 hashFn: @compileError("anytype fields are removed from the language"),
301 params: @compileError("anytype fields are removed from the language"),
300 ty: type,
301 params: *const anyopaque,
302302 name: []const u8,
303303};
304304const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };
305305const pwhashes = [_]CryptoPwhash{
306 .{ .hashFn = crypto.pwhash.bcrypt.strHash, .params = bcrypt_params, .name = "bcrypt" },
306 .{ .ty = crypto.pwhash.bcrypt, .params = &bcrypt_params, .name = "bcrypt" },
307307 .{
308 .hashFn = crypto.pwhash.scrypt.strHash,
309 .params = crypto.pwhash.scrypt.Params.interactive,
308 .ty = crypto.pwhash.scrypt,
309 .params = &crypto.pwhash.scrypt.Params.interactive,
310310 .name = "scrypt",
311311 },
312312 .{
313 .hashFn = crypto.pwhash.argon2.strHash,
314 .params = crypto.pwhash.argon2.Params.interactive_2id,
313 .ty = crypto.pwhash.argon2,
314 .params = &crypto.pwhash.argon2.Params.interactive_2id,
315315 .name = "argon2",
316316 },
317317};
318318
319319fn benchmarkPwhash(
320 comptime hashFn: anytype,
321 comptime params: anytype,
320 comptime ty: anytype,
321 comptime params: *const anyopaque,
322322 comptime count: comptime_int,
323323) !f64 {
324324 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 };
326326 var buf: [256]u8 = undefined;
327327
328328 var timer = try Timer.start();
......@@ -330,7 +330,7 @@ fn benchmarkPwhash(
330330 {
331331 var i: usize = 0;
332332 while (i < count) : (i += 1) {
333 _ = try hashFn(password, opts, &buf);
333 _ = try ty.strHash(password, opts, &buf);
334334 mem.doNotOptimizeAway(&buf);
335335 }
336336 }
......@@ -463,8 +463,8 @@ pub fn main() !void {
463463
464464 inline for (pwhashes) |H| {
465465 if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
466 const throughput = try benchmarkPwhash(H.hashFn, H.params, mode(64));
467 try stdout.print("{s:>17}: {d:.3} s/ops\n", .{ H.name, throughput });
466 const throughput = try benchmarkPwhash(H.ty, H.params, mode(64));
467 try stdout.print("{s:>17}: {d:10.3} s/ops\n", .{ H.name, throughput });
468468 }
469469 }
470470}
lib/std/hash/benchmark.zig+1-1
......@@ -230,7 +230,7 @@ pub fn main() !void {
230230 inline for (hashes) |H| {
231231 if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
232232 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
235235 // Always reseed prior to every call so we are hashing the same buffer contents.
236236 // This allows easier comparison between different implementations.