| author | |
| committer | |
| log | 48410943cbcbf53ae3c9895b2452218d6eacbc4b |
| tree | 7bc17655da80359c234584ed45289471cb727194 |
| parent | c050ec4e570caf53be924bcbbe4194512b6a022c |
Changed CRC api so the polynomial is specified as an enum for simpler
construction.2 files changed, 86 insertions(+), 30 deletions(-)
std/hash/benchmark.zig+73-17| ... | @@ -15,6 +15,7 @@ var prng = std.rand.DefaultPrng.init(0); | ... | @@ -15,6 +15,7 @@ var prng = std.rand.DefaultPrng.init(0); |
| 15 | const Hash = struct { | 15 | const Hash = struct { |
| 16 | ty: type, | 16 | ty: type, |
| 17 | name: []const u8, | 17 | name: []const u8, |
| 18 | has_iterative_api: bool = true, | ||
| 18 | init_u8s: ?[]const u8 = null, | 19 | init_u8s: ?[]const u8 = null, |
| 19 | init_u64: ?u64 = null, | 20 | init_u64: ?u64 = null, |
| 20 | }; | 21 | }; |
| ... | @@ -22,11 +23,62 @@ const Hash = struct { | ... | @@ -22,11 +23,62 @@ const Hash = struct { |
| 22 | const siphash_key = "0123456789abcdef"; | 23 | const siphash_key = "0123456789abcdef"; |
| 23 | 24 | ||
| 24 | const hashes = [_]Hash{ | 25 | const hashes = [_]Hash{ |
| 25 | Hash{ .ty = hash.Wyhash, .name = "wyhash", .init_u64 = 0 }, | 26 | Hash{ |
| 26 | Hash{ .ty = hash.SipHash64(1, 3), .name = "siphash(1,3)", .init_u8s = siphash_key }, | 27 | .ty = hash.Wyhash, |
| 27 | Hash{ .ty = hash.SipHash64(2, 4), .name = "siphash(2,4)", .init_u8s = siphash_key }, | 28 | .name = "wyhash", |
| 28 | Hash{ .ty = hash.Fnv1a_64, .name = "fnv1a" }, | 29 | .init_u64 = 0, |
| 29 | Hash{ .ty = hash.Crc32, .name = "crc32" }, | 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 | const Result = struct { | 84 | const Result = struct { |
| ... | @@ -78,10 +130,8 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R | ... | @@ -78,10 +130,8 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R |
| 78 | 130 | ||
| 79 | var sum: u64 = 0; | 131 | var sum: u64 = 0; |
| 80 | while (i < key_count) : (i += 1) { | 132 | while (i < key_count) : (i += 1) { |
| 81 | const o = i % (block_size - key_size); | 133 | const small_key = block[0..key_size]; |
| 82 | const small_key = block[o .. key_size + o]; | 134 | sum +%= blk: { |
| 83 | |||
| 84 | const result = blk: { | ||
| 85 | if (H.init_u8s) |init| { | 135 | if (H.init_u8s) |init| { |
| 86 | break :blk H.ty.hash(init, small_key); | 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,8 +140,6 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R |
| 90 | } | 140 | } |
| 91 | break :blk H.ty.hash(small_key); | 141 | break :blk H.ty.hash(small_key); |
| 92 | }; | 142 | }; |
| 93 | |||
| 94 | sum +%= result; | ||
| 95 | } | 143 | } |
| 96 | const end = timer.read(); | 144 | const end = timer.read(); |
| 97 | 145 | ||
| ... | @@ -142,6 +190,7 @@ pub fn main() !void { | ... | @@ -142,6 +190,7 @@ pub fn main() !void { |
| 142 | var filter: ?[]u8 = ""; | 190 | var filter: ?[]u8 = ""; |
| 143 | var count: usize = mode(128 * MiB); | 191 | var count: usize = mode(128 * MiB); |
| 144 | var key_size: usize = 32; | 192 | var key_size: usize = 32; |
| 193 | var seed: u32 = 0; | ||
| 145 | 194 | ||
| 146 | var i: usize = 1; | 195 | var i: usize = 1; |
| 147 | while (i < args.len) : (i += 1) { | 196 | while (i < args.len) : (i += 1) { |
| ... | @@ -155,8 +204,8 @@ pub fn main() !void { | ... | @@ -155,8 +204,8 @@ pub fn main() !void { |
| 155 | std.os.exit(1); | 204 | std.os.exit(1); |
| 156 | } | 205 | } |
| 157 | 206 | ||
| 158 | const seed = try std.fmt.parseUnsigned(u32, args[i], 10); | 207 | seed = try std.fmt.parseUnsigned(u32, args[i], 10); |
| 159 | prng.seed(seed); | 208 | // we seed later |
| 160 | } else if (std.mem.eql(u8, args[i], "--filter")) { | 209 | } else if (std.mem.eql(u8, args[i], "--filter")) { |
| 161 | i += 1; | 210 | i += 1; |
| 162 | if (i == args.len) { | 211 | if (i == args.len) { |
| ... | @@ -197,11 +246,18 @@ pub fn main() !void { | ... | @@ -197,11 +246,18 @@ pub fn main() !void { |
| 197 | 246 | ||
| 198 | inline for (hashes) |H| { | 247 | inline for (hashes) |H| { |
| 199 | if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { | 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 | try stdout.print("{}\n", H.name); | 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 | try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash); | 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 | } |
std/hash/crc.zig+13-13| ... | @@ -9,17 +9,17 @@ const std = @import("../std.zig"); | ... | @@ -9,17 +9,17 @@ const std = @import("../std.zig"); |
| 9 | const debug = std.debug; | 9 | const debug = std.debug; |
| 10 | const testing = std.testing; | 10 | const testing = std.testing; |
| 11 | 11 | ||
| 12 | pub const Polynomial = struct { | 12 | pub const Polynomial = enum(u32) { |
| 13 | const IEEE = 0xedb88320; | 13 | IEEE = 0xedb88320, |
| 14 | const Castagnoli = 0x82f63b78; | 14 | Castagnoli = 0x82f63b78, |
| 15 | const Koopman = 0xeb31d82e; | 15 | Koopman = 0xeb31d82e, |
| 16 | }; | 16 | }; |
| 17 | 17 | ||
| 18 | // IEEE is by far the most common CRC and so is aliased by default. | 18 | // IEEE is by far the most common CRC and so is aliased by default. |
| 19 | pub const Crc32 = Crc32WithPoly(Polynomial.IEEE); | 19 | pub const Crc32 = Crc32WithPoly(.IEEE); |
| 20 | 20 | ||
| 21 | // slicing-by-8 crc32 implementation. | 21 | // slicing-by-8 crc32 implementation. |
| 22 | pub fn Crc32WithPoly(comptime poly: u32) type { | 22 | pub fn Crc32WithPoly(comptime poly: Polynomial) type { |
| 23 | return struct { | 23 | return struct { |
| 24 | const Self = @This(); | 24 | const Self = @This(); |
| 25 | const lookup_tables = comptime block: { | 25 | const lookup_tables = comptime block: { |
| ... | @@ -31,7 +31,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type { | ... | @@ -31,7 +31,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type { |
| 31 | var j: usize = 0; | 31 | var j: usize = 0; |
| 32 | while (j < 8) : (j += 1) { | 32 | while (j < 8) : (j += 1) { |
| 33 | if (crc & 1 == 1) { | 33 | if (crc & 1 == 1) { |
| 34 | crc = (crc >> 1) ^ poly; | 34 | crc = (crc >> 1) ^ @enumToInt(poly); |
| 35 | } else { | 35 | } else { |
| 36 | crc = (crc >> 1); | 36 | crc = (crc >> 1); |
| 37 | } | 37 | } |
| ... | @@ -100,7 +100,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type { | ... | @@ -100,7 +100,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type { |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | test "crc32 ieee" { | 102 | test "crc32 ieee" { |
| 103 | const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE); | 103 | const Crc32Ieee = Crc32WithPoly(.IEEE); |
| 104 | 104 | ||
| 105 | testing.expect(Crc32Ieee.hash("") == 0x00000000); | 105 | testing.expect(Crc32Ieee.hash("") == 0x00000000); |
| 106 | testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43); | 106 | testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43); |
| ... | @@ -108,7 +108,7 @@ test "crc32 ieee" { | ... | @@ -108,7 +108,7 @@ test "crc32 ieee" { |
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | test "crc32 castagnoli" { | 110 | test "crc32 castagnoli" { |
| 111 | const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli); | 111 | const Crc32Castagnoli = Crc32WithPoly(.Castagnoli); |
| 112 | 112 | ||
| 113 | testing.expect(Crc32Castagnoli.hash("") == 0x00000000); | 113 | testing.expect(Crc32Castagnoli.hash("") == 0x00000000); |
| 114 | testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330); | 114 | testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330); |
| ... | @@ -116,7 +116,7 @@ test "crc32 castagnoli" { | ... | @@ -116,7 +116,7 @@ test "crc32 castagnoli" { |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | // half-byte lookup table implementation. | 118 | // half-byte lookup table implementation. |
| 119 | pub fn Crc32SmallWithPoly(comptime poly: u32) type { | 119 | pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type { |
| 120 | return struct { | 120 | return struct { |
| 121 | const Self = @This(); | 121 | const Self = @This(); |
| 122 | const lookup_table = comptime block: { | 122 | const lookup_table = comptime block: { |
| ... | @@ -127,7 +127,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type { | ... | @@ -127,7 +127,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type { |
| 127 | var j: usize = 0; | 127 | var j: usize = 0; |
| 128 | while (j < 8) : (j += 1) { | 128 | while (j < 8) : (j += 1) { |
| 129 | if (crc & 1 == 1) { | 129 | if (crc & 1 == 1) { |
| 130 | crc = (crc >> 1) ^ poly; | 130 | crc = (crc >> 1) ^ @enumToInt(poly); |
| 131 | } else { | 131 | } else { |
| 132 | crc = (crc >> 1); | 132 | crc = (crc >> 1); |
| 133 | } | 133 | } |
| ... | @@ -164,7 +164,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type { | ... | @@ -164,7 +164,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type { |
| 164 | } | 164 | } |
| 165 | 165 | ||
| 166 | test "small crc32 ieee" { | 166 | test "small crc32 ieee" { |
| 167 | const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE); | 167 | const Crc32Ieee = Crc32SmallWithPoly(.IEEE); |
| 168 | 168 | ||
| 169 | testing.expect(Crc32Ieee.hash("") == 0x00000000); | 169 | testing.expect(Crc32Ieee.hash("") == 0x00000000); |
| 170 | testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43); | 170 | testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43); |
| ... | @@ -172,7 +172,7 @@ test "small crc32 ieee" { | ... | @@ -172,7 +172,7 @@ test "small crc32 ieee" { |
| 172 | } | 172 | } |
| 173 | 173 | ||
| 174 | test "small crc32 castagnoli" { | 174 | test "small crc32 castagnoli" { |
| 175 | const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli); | 175 | const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli); |
| 176 | 176 | ||
| 177 | testing.expect(Crc32Castagnoli.hash("") == 0x00000000); | 177 | testing.expect(Crc32Castagnoli.hash("") == 0x00000000); |
| 178 | testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330); | 178 | testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330); |