| ... | ... | @@ -15,6 +15,7 @@ var prng = std.rand.DefaultPrng.init(0); |
| 15 | 15 | const Hash = struct { |
| 16 | 16 | ty: type, |
| 17 | 17 | name: []const u8, |
| 18 | has_iterative_api: bool = true, |
| 18 | 19 | init_u8s: ?[]const u8 = null, |
| 19 | 20 | init_u64: ?u64 = null, |
| 20 | 21 | }; |
| ... | ... | @@ -22,11 +23,62 @@ const Hash = struct { |
| 22 | 23 | const siphash_key = "0123456789abcdef"; |
| 23 | 24 | |
| 24 | 25 | const hashes = [_]Hash{ |
| 25 | | Hash{ .ty = hash.Wyhash, .name = "wyhash", .init_u64 = 0 }, |
| 26 | | Hash{ .ty = hash.SipHash64(1, 3), .name = "siphash(1,3)", .init_u8s = siphash_key }, |
| 27 | | Hash{ .ty = hash.SipHash64(2, 4), .name = "siphash(2,4)", .init_u8s = siphash_key }, |
| 28 | | Hash{ .ty = hash.Fnv1a_64, .name = "fnv1a" }, |
| 29 | | Hash{ .ty = hash.Crc32, .name = "crc32" }, |
| 26 | Hash{ |
| 27 | .ty = hash.Wyhash, |
| 28 | .name = "wyhash", |
| 29 | .init_u64 = 0, |
| 30 | }, |
| 31 | Hash{ |
| 32 | .ty = hash.SipHash64(1, 3), |
| 33 | .name = "siphash(1,3)", |
| 34 | .init_u8s = siphash_key, |
| 35 | }, |
| 36 | Hash{ |
| 37 | .ty = hash.SipHash64(2, 4), |
| 38 | .name = "siphash(2,4)", |
| 39 | .init_u8s = siphash_key, |
| 40 | }, |
| 41 | Hash{ |
| 42 | .ty = hash.Fnv1a_64, |
| 43 | .name = "fnv1a", |
| 44 | }, |
| 45 | Hash{ |
| 46 | .ty = hash.Adler32, |
| 47 | .name = "adler32", |
| 48 | }, |
| 49 | Hash{ |
| 50 | .ty = hash.crc.Crc32WithPoly(.IEEE), |
| 51 | .name = "crc32-slicing-by-8", |
| 52 | }, |
| 53 | Hash{ |
| 54 | .ty = hash.crc.Crc32SmallWithPoly(.IEEE), |
| 55 | .name = "crc32-half-byte-lookup", |
| 56 | }, |
| 57 | Hash{ |
| 58 | .ty = hash.CityHash32, |
| 59 | .name = "cityhash-32", |
| 60 | .has_iterative_api = false, |
| 61 | }, |
| 62 | Hash{ |
| 63 | .ty = hash.CityHash64, |
| 64 | .name = "cityhash-64", |
| 65 | .has_iterative_api = false, |
| 66 | }, |
| 67 | Hash{ |
| 68 | .ty = hash.Murmur2_32, |
| 69 | .name = "murmur2-32", |
| 70 | .has_iterative_api = false, |
| 71 | }, |
| 72 | Hash{ |
| 73 | .ty = hash.Murmur2_64, |
| 74 | .name = "murmur2-64", |
| 75 | .has_iterative_api = false, |
| 76 | }, |
| 77 | Hash{ |
| 78 | .ty = hash.Murmur3_32, |
| 79 | .name = "murmur3-32", |
| 80 | .has_iterative_api = false, |
| 81 | }, |
| 30 | 82 | }; |
| 31 | 83 | |
| 32 | 84 | const Result = struct { |
| ... | ... | @@ -78,10 +130,8 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R |
| 78 | 130 | |
| 79 | 131 | var sum: u64 = 0; |
| 80 | 132 | while (i < key_count) : (i += 1) { |
| 81 | | const o = i % (block_size - key_size); |
| 82 | | const small_key = block[o .. key_size + o]; |
| 83 | | |
| 84 | | const result = blk: { |
| 133 | const small_key = block[0..key_size]; |
| 134 | sum +%= blk: { |
| 85 | 135 | if (H.init_u8s) |init| { |
| 86 | 136 | break :blk H.ty.hash(init, small_key); |
| 87 | 137 | } |
| ... | ... | @@ -90,8 +140,6 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R |
| 90 | 140 | } |
| 91 | 141 | break :blk H.ty.hash(small_key); |
| 92 | 142 | }; |
| 93 | | |
| 94 | | sum +%= result; |
| 95 | 143 | } |
| 96 | 144 | const end = timer.read(); |
| 97 | 145 | |
| ... | ... | @@ -142,6 +190,7 @@ pub fn main() !void { |
| 142 | 190 | var filter: ?[]u8 = ""; |
| 143 | 191 | var count: usize = mode(128 * MiB); |
| 144 | 192 | var key_size: usize = 32; |
| 193 | var seed: u32 = 0; |
| 145 | 194 | |
| 146 | 195 | var i: usize = 1; |
| 147 | 196 | while (i < args.len) : (i += 1) { |
| ... | ... | @@ -155,8 +204,8 @@ pub fn main() !void { |
| 155 | 204 | std.os.exit(1); |
| 156 | 205 | } |
| 157 | 206 | |
| 158 | | const seed = try std.fmt.parseUnsigned(u32, args[i], 10); |
| 159 | | prng.seed(seed); |
| 207 | seed = try std.fmt.parseUnsigned(u32, args[i], 10); |
| 208 | // we seed later |
| 160 | 209 | } else if (std.mem.eql(u8, args[i], "--filter")) { |
| 161 | 210 | i += 1; |
| 162 | 211 | if (i == args.len) { |
| ... | ... | @@ -197,11 +246,18 @@ pub fn main() !void { |
| 197 | 246 | |
| 198 | 247 | inline for (hashes) |H| { |
| 199 | 248 | if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { |
| 200 | | const result = try benchmarkHash(H, count); |
| 201 | | const result_small = try benchmarkHashSmallKeys(H, key_size, count); |
| 202 | | |
| 203 | 249 | try stdout.print("{}\n", H.name); |
| 204 | | try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash); |
| 250 | |
| 251 | // Always reseed prior to every call so we are hashing the same buffer contents. |
| 252 | // This allows easier comparison between different implementations. |
| 253 | if (H.has_iterative_api) { |
| 254 | prng.seed(seed); |
| 255 | const result = try benchmarkHash(H, count); |
| 256 | try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash); |
| 257 | } |
| 258 | |
| 259 | prng.seed(seed); |
| 260 | const result_small = try benchmarkHashSmallKeys(H, key_size, count); |
| 205 | 261 | try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash); |
| 206 | 262 | } |
| 207 | 263 | } |