authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-08-21 20:46:15+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2019-08-21 20:46:15+12:00
log48410943cbcbf53ae3c9895b2452218d6eacbc4b
tree7bc17655da80359c234584ed45289471cb727194
parentc050ec4e570caf53be924bcbbe4194512b6a022c

Add more hash functions to benchmark scripts

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);
1515const Hash = struct {
1616 ty: type,
1717 name: []const u8,
18 has_iterative_api: bool = true,
1819 init_u8s: ?[]const u8 = null,
1920 init_u64: ?u64 = null,
2021};
......@@ -22,11 +23,62 @@ const Hash = struct {
2223const siphash_key = "0123456789abcdef";
2324
2425const 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 },
3082};
3183
3284const Result = struct {
......@@ -78,10 +130,8 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R
78130
79131 var sum: u64 = 0;
80132 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: {
85135 if (H.init_u8s) |init| {
86136 break :blk H.ty.hash(init, small_key);
87137 }
......@@ -90,8 +140,6 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R
90140 }
91141 break :blk H.ty.hash(small_key);
92142 };
93
94 sum +%= result;
95143 }
96144 const end = timer.read();
97145
......@@ -142,6 +190,7 @@ pub fn main() !void {
142190 var filter: ?[]u8 = "";
143191 var count: usize = mode(128 * MiB);
144192 var key_size: usize = 32;
193 var seed: u32 = 0;
145194
146195 var i: usize = 1;
147196 while (i < args.len) : (i += 1) {
......@@ -155,8 +204,8 @@ pub fn main() !void {
155204 std.os.exit(1);
156205 }
157206
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
160209 } else if (std.mem.eql(u8, args[i], "--filter")) {
161210 i += 1;
162211 if (i == args.len) {
......@@ -197,11 +246,18 @@ pub fn main() !void {
197246
198247 inline for (hashes) |H| {
199248 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
203249 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);
205261 try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash);
206262 }
207263 }
std/hash/crc.zig+13-13
......@@ -9,17 +9,17 @@ const std = @import("../std.zig");
99const debug = std.debug;
1010const testing = std.testing;
1111
12pub const Polynomial = struct {
13 const IEEE = 0xedb88320;
14 const Castagnoli = 0x82f63b78;
15 const Koopman = 0xeb31d82e;
12pub const Polynomial = enum(u32) {
13 IEEE = 0xedb88320,
14 Castagnoli = 0x82f63b78,
15 Koopman = 0xeb31d82e,
1616};
1717
1818// IEEE is by far the most common CRC and so is aliased by default.
19pub const Crc32 = Crc32WithPoly(Polynomial.IEEE);
19pub const Crc32 = Crc32WithPoly(.IEEE);
2020
2121// slicing-by-8 crc32 implementation.
22pub fn Crc32WithPoly(comptime poly: u32) type {
22pub fn Crc32WithPoly(comptime poly: Polynomial) type {
2323 return struct {
2424 const Self = @This();
2525 const lookup_tables = comptime block: {
......@@ -31,7 +31,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
3131 var j: usize = 0;
3232 while (j < 8) : (j += 1) {
3333 if (crc & 1 == 1) {
34 crc = (crc >> 1) ^ poly;
34 crc = (crc >> 1) ^ @enumToInt(poly);
3535 } else {
3636 crc = (crc >> 1);
3737 }
......@@ -100,7 +100,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
100100}
101101
102102test "crc32 ieee" {
103 const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE);
103 const Crc32Ieee = Crc32WithPoly(.IEEE);
104104
105105 testing.expect(Crc32Ieee.hash("") == 0x00000000);
106106 testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
......@@ -108,7 +108,7 @@ test "crc32 ieee" {
108108}
109109
110110test "crc32 castagnoli" {
111 const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli);
111 const Crc32Castagnoli = Crc32WithPoly(.Castagnoli);
112112
113113 testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
114114 testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
......@@ -116,7 +116,7 @@ test "crc32 castagnoli" {
116116}
117117
118118// half-byte lookup table implementation.
119pub fn Crc32SmallWithPoly(comptime poly: u32) type {
119pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
120120 return struct {
121121 const Self = @This();
122122 const lookup_table = comptime block: {
......@@ -127,7 +127,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
127127 var j: usize = 0;
128128 while (j < 8) : (j += 1) {
129129 if (crc & 1 == 1) {
130 crc = (crc >> 1) ^ poly;
130 crc = (crc >> 1) ^ @enumToInt(poly);
131131 } else {
132132 crc = (crc >> 1);
133133 }
......@@ -164,7 +164,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
164164}
165165
166166test "small crc32 ieee" {
167 const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE);
167 const Crc32Ieee = Crc32SmallWithPoly(.IEEE);
168168
169169 testing.expect(Crc32Ieee.hash("") == 0x00000000);
170170 testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
......@@ -172,7 +172,7 @@ test "small crc32 ieee" {
172172}
173173
174174test "small crc32 castagnoli" {
175 const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli);
175 const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli);
176176
177177 testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
178178 testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);