aboutsummaryrefslogtreecommitdiff
path: root/std/hash
diff options
context:
space:
mode:
Diffstat (limited to 'std/hash')
-rw-r--r--std/hash/benchmark.zig273
-rw-r--r--std/hash/crc.zig26
-rw-r--r--std/hash/siphash.zig4
-rw-r--r--std/hash/throughput_test.zig148
-rw-r--r--std/hash/wyhash.zig4
5 files changed, 290 insertions, 165 deletions
diff --git a/std/hash/benchmark.zig b/std/hash/benchmark.zig
new file mode 100644
index 0000000000..f09d431fce
--- /dev/null
+++ b/std/hash/benchmark.zig
@@ -0,0 +1,273 @@
+// zig run benchmark.zig --release-fast --override-std-dir ..
+
+const builtin = @import("builtin");
+const std = @import("std");
+const time = std.time;
+const Timer = time.Timer;
+const hash = std.hash;
+
+const KiB = 1024;
+const MiB = 1024 * KiB;
+const GiB = 1024 * MiB;
+
+var prng = std.rand.DefaultPrng.init(0);
+
+const Hash = struct {
+ ty: type,
+ name: []const u8,
+ has_iterative_api: bool = true,
+ init_u8s: ?[]const u8 = null,
+ init_u64: ?u64 = null,
+};
+
+const siphash_key = "0123456789abcdef";
+
+const hashes = [_]Hash{
+ Hash{
+ .ty = hash.Wyhash,
+ .name = "wyhash",
+ .init_u64 = 0,
+ },
+ Hash{
+ .ty = hash.SipHash64(1, 3),
+ .name = "siphash(1,3)",
+ .init_u8s = siphash_key,
+ },
+ Hash{
+ .ty = hash.SipHash64(2, 4),
+ .name = "siphash(2,4)",
+ .init_u8s = siphash_key,
+ },
+ Hash{
+ .ty = hash.Fnv1a_64,
+ .name = "fnv1a",
+ },
+ Hash{
+ .ty = hash.Adler32,
+ .name = "adler32",
+ },
+ Hash{
+ .ty = hash.crc.Crc32WithPoly(.IEEE),
+ .name = "crc32-slicing-by-8",
+ },
+ Hash{
+ .ty = hash.crc.Crc32SmallWithPoly(.IEEE),
+ .name = "crc32-half-byte-lookup",
+ },
+ Hash{
+ .ty = hash.CityHash32,
+ .name = "cityhash-32",
+ .has_iterative_api = false,
+ },
+ Hash{
+ .ty = hash.CityHash64,
+ .name = "cityhash-64",
+ .has_iterative_api = false,
+ },
+ Hash{
+ .ty = hash.Murmur2_32,
+ .name = "murmur2-32",
+ .has_iterative_api = false,
+ },
+ Hash{
+ .ty = hash.Murmur2_64,
+ .name = "murmur2-64",
+ .has_iterative_api = false,
+ },
+ Hash{
+ .ty = hash.Murmur3_32,
+ .name = "murmur3-32",
+ .has_iterative_api = false,
+ },
+};
+
+const Result = struct {
+ hash: u64,
+ throughput: u64,
+};
+
+const block_size: usize = 8192;
+
+pub fn benchmarkHash(comptime H: var, bytes: usize) !Result {
+ var h = blk: {
+ if (H.init_u8s) |init| {
+ break :blk H.ty.init(init);
+ }
+ if (H.init_u64) |init| {
+ break :blk H.ty.init(init);
+ }
+ break :blk H.ty.init();
+ };
+
+ var block: [block_size]u8 = undefined;
+ prng.random.bytes(block[0..]);
+
+ var offset: usize = 0;
+ var timer = try Timer.start();
+ const start = timer.lap();
+ while (offset < bytes) : (offset += block.len) {
+ h.update(block[0..]);
+ }
+ const end = timer.read();
+
+ const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
+ const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
+
+ return Result{
+ .hash = h.final(),
+ .throughput = throughput,
+ };
+}
+
+pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !Result {
+ const key_count = bytes / key_size;
+ var block: [block_size]u8 = undefined;
+ prng.random.bytes(block[0..]);
+
+ var i: usize = 0;
+ var timer = try Timer.start();
+ const start = timer.lap();
+
+ var sum: u64 = 0;
+ while (i < key_count) : (i += 1) {
+ const small_key = block[0..key_size];
+ sum +%= blk: {
+ if (H.init_u8s) |init| {
+ break :blk H.ty.hash(init, small_key);
+ }
+ if (H.init_u64) |init| {
+ break :blk H.ty.hash(init, small_key);
+ }
+ break :blk H.ty.hash(small_key);
+ };
+ }
+ const end = timer.read();
+
+ const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
+ const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
+
+ return Result{
+ .hash = sum,
+ .throughput = throughput,
+ };
+}
+
+fn usage() void {
+ std.debug.warn(
+ \\throughput_test [options]
+ \\
+ \\Options:
+ \\ --filter [test-name]
+ \\ --seed [int]
+ \\ --count [int]
+ \\ --key-size [int]
+ \\ --iterative-only
+ \\ --help
+ \\
+ );
+}
+
+fn mode(comptime x: comptime_int) comptime_int {
+ return if (builtin.mode == builtin.Mode.Debug) x / 64 else x;
+}
+
+// TODO(#1358): Replace with builtin formatted padding when available.
+fn printPad(stdout: var, s: []const u8) !void {
+ var i: usize = 0;
+ while (i < 12 - s.len) : (i += 1) {
+ try stdout.print(" ");
+ }
+ try stdout.print("{}", s);
+}
+
+pub fn main() !void {
+ var stdout_file = try std.io.getStdOut();
+ var stdout_out_stream = stdout_file.outStream();
+ const stdout = &stdout_out_stream.stream;
+
+ var buffer: [1024]u8 = undefined;
+ var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
+ const args = try std.process.argsAlloc(&fixed.allocator);
+
+ var filter: ?[]u8 = "";
+ var count: usize = mode(128 * MiB);
+ var key_size: usize = 32;
+ var seed: u32 = 0;
+ var test_iterative_only = false;
+
+ var i: usize = 1;
+ while (i < args.len) : (i += 1) {
+ if (std.mem.eql(u8, args[i], "--mode")) {
+ try stdout.print("{}\n", builtin.mode);
+ return;
+ } else if (std.mem.eql(u8, args[i], "--seed")) {
+ i += 1;
+ if (i == args.len) {
+ usage();
+ std.os.exit(1);
+ }
+
+ seed = try std.fmt.parseUnsigned(u32, args[i], 10);
+ // we seed later
+ } else if (std.mem.eql(u8, args[i], "--filter")) {
+ i += 1;
+ if (i == args.len) {
+ usage();
+ std.os.exit(1);
+ }
+
+ filter = args[i];
+ } else if (std.mem.eql(u8, args[i], "--count")) {
+ i += 1;
+ if (i == args.len) {
+ usage();
+ std.os.exit(1);
+ }
+
+ const c = try std.fmt.parseUnsigned(usize, args[i], 10);
+ count = c * MiB;
+ } else if (std.mem.eql(u8, args[i], "--key-size")) {
+ i += 1;
+ if (i == args.len) {
+ usage();
+ std.os.exit(1);
+ }
+
+ key_size = try std.fmt.parseUnsigned(usize, args[i], 10);
+ if (key_size > block_size) {
+ try stdout.print("key_size cannot exceed block size of {}\n", block_size);
+ std.os.exit(1);
+ }
+ } else if (std.mem.eql(u8, args[i], "--iterative-only")) {
+ test_iterative_only = true;
+ } else if (std.mem.eql(u8, args[i], "--help")) {
+ usage();
+ return;
+ } else {
+ usage();
+ std.os.exit(1);
+ }
+ }
+
+ inline for (hashes) |H| {
+ if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
+ if (!test_iterative_only or H.has_iterative_api) {
+ try stdout.print("{}\n", H.name);
+
+ // Always reseed prior to every call so we are hashing the same buffer contents.
+ // This allows easier comparison between different implementations.
+ if (H.has_iterative_api) {
+ prng.seed(seed);
+ const result = try benchmarkHash(H, count);
+ try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash);
+ }
+
+ if (!test_iterative_only) {
+ prng.seed(seed);
+ const result_small = try benchmarkHashSmallKeys(H, key_size, count);
+ try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash);
+ }
+ }
+ }
+ }
+}
diff --git a/std/hash/crc.zig b/std/hash/crc.zig
index 53b4262c93..73e5bb0371 100644
--- a/std/hash/crc.zig
+++ b/std/hash/crc.zig
@@ -9,17 +9,17 @@ const std = @import("../std.zig");
const debug = std.debug;
const testing = std.testing;
-pub const Polynomial = struct {
- const IEEE = 0xedb88320;
- const Castagnoli = 0x82f63b78;
- const Koopman = 0xeb31d82e;
+pub const Polynomial = enum(u32) {
+ IEEE = 0xedb88320,
+ Castagnoli = 0x82f63b78,
+ Koopman = 0xeb31d82e,
};
// IEEE is by far the most common CRC and so is aliased by default.
-pub const Crc32 = Crc32WithPoly(Polynomial.IEEE);
+pub const Crc32 = Crc32WithPoly(.IEEE);
// slicing-by-8 crc32 implementation.
-pub fn Crc32WithPoly(comptime poly: u32) type {
+pub fn Crc32WithPoly(comptime poly: Polynomial) type {
return struct {
const Self = @This();
const lookup_tables = comptime block: {
@@ -31,7 +31,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
- crc = (crc >> 1) ^ poly;
+ crc = (crc >> 1) ^ @enumToInt(poly);
} else {
crc = (crc >> 1);
}
@@ -100,7 +100,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
}
test "crc32 ieee" {
- const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE);
+ const Crc32Ieee = Crc32WithPoly(.IEEE);
testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
@@ -108,7 +108,7 @@ test "crc32 ieee" {
}
test "crc32 castagnoli" {
- const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli);
+ const Crc32Castagnoli = Crc32WithPoly(.Castagnoli);
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
@@ -116,7 +116,7 @@ test "crc32 castagnoli" {
}
// half-byte lookup table implementation.
-pub fn Crc32SmallWithPoly(comptime poly: u32) type {
+pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type {
return struct {
const Self = @This();
const lookup_table = comptime block: {
@@ -127,7 +127,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
var j: usize = 0;
while (j < 8) : (j += 1) {
if (crc & 1 == 1) {
- crc = (crc >> 1) ^ poly;
+ crc = (crc >> 1) ^ @enumToInt(poly);
} else {
crc = (crc >> 1);
}
@@ -164,7 +164,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
}
test "small crc32 ieee" {
- const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE);
+ const Crc32Ieee = Crc32SmallWithPoly(.IEEE);
testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
@@ -172,7 +172,7 @@ test "small crc32 ieee" {
}
test "small crc32 castagnoli" {
- const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli);
+ const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli);
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
diff --git a/std/hash/siphash.zig b/std/hash/siphash.zig
index 8e83d67897..aa38c61863 100644
--- a/std/hash/siphash.zig
+++ b/std/hash/siphash.zig
@@ -152,8 +152,8 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
pub fn hash(key: []const u8, input: []const u8) T {
var c = Self.init(key);
- c.update(input);
- return c.final();
+ @inlineCall(c.update, input);
+ return @inlineCall(c.final);
}
};
}
diff --git a/std/hash/throughput_test.zig b/std/hash/throughput_test.zig
deleted file mode 100644
index 4b7e8ef344..0000000000
--- a/std/hash/throughput_test.zig
+++ /dev/null
@@ -1,148 +0,0 @@
-const builtin = @import("builtin");
-const std = @import("std");
-const time = std.time;
-const Timer = time.Timer;
-const hash = std.hash;
-
-const KiB = 1024;
-const MiB = 1024 * KiB;
-const GiB = 1024 * MiB;
-
-var prng = std.rand.DefaultPrng.init(0);
-
-const Hash = struct {
- ty: type,
- name: []const u8,
- init_u8s: ?[]const u8 = null,
- init_u64: ?u64 = null,
-};
-
-const siphash_key = "0123456789abcdef";
-
-const hashes = [_]Hash{
- Hash{ .ty = hash.Wyhash, .name = "wyhash", .init_u64 = 0 },
- Hash{ .ty = hash.SipHash64(1, 3), .name = "siphash(1,3)", .init_u8s = siphash_key },
- Hash{ .ty = hash.SipHash64(2, 4), .name = "siphash(2,4)", .init_u8s = siphash_key },
- Hash{ .ty = hash.Fnv1a_64, .name = "fnv1a" },
- Hash{ .ty = hash.Crc32, .name = "crc32" },
-};
-
-const Result = struct {
- hash: u64,
- throughput: u64,
-};
-
-pub fn benchmarkHash(comptime H: var, bytes: usize) !Result {
- var h = blk: {
- if (H.init_u8s) |init| {
- break :blk H.ty.init(init);
- }
- if (H.init_u64) |init| {
- break :blk H.ty.init(init);
- }
- break :blk H.ty.init();
- };
-
- var block: [8192]u8 = undefined;
- prng.random.bytes(block[0..]);
-
- var offset: usize = 0;
- var timer = try Timer.start();
- const start = timer.lap();
- while (offset < bytes) : (offset += block.len) {
- h.update(block[0..]);
- }
- const end = timer.read();
-
- const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
- const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
-
- return Result{
- .hash = h.final(),
- .throughput = throughput,
- };
-}
-
-fn usage() void {
- std.debug.warn(
- \\throughput_test [options]
- \\
- \\Options:
- \\ --filter [test-name]
- \\ --seed [int]
- \\ --count [int]
- \\ --help
- \\
- );
-}
-
-fn mode(comptime x: comptime_int) comptime_int {
- return if (builtin.mode == builtin.Mode.Debug) x / 64 else x;
-}
-
-// TODO(#1358): Replace with builtin formatted padding when available.
-fn printPad(stdout: var, s: []const u8) !void {
- var i: usize = 0;
- while (i < 12 - s.len) : (i += 1) {
- try stdout.print(" ");
- }
- try stdout.print("{}", s);
-}
-
-pub fn main() !void {
- var stdout_file = try std.io.getStdOut();
- var stdout_out_stream = stdout_file.outStream();
- const stdout = &stdout_out_stream.stream;
-
- var buffer: [1024]u8 = undefined;
- var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
- const args = try std.process.argsAlloc(&fixed.allocator);
-
- var filter: ?[]u8 = "";
- var count: usize = mode(128 * MiB);
-
- var i: usize = 1;
- while (i < args.len) : (i += 1) {
- if (std.mem.eql(u8, args[i], "--seed")) {
- i += 1;
- if (i == args.len) {
- usage();
- std.os.exit(1);
- }
-
- const seed = try std.fmt.parseUnsigned(u32, args[i], 10);
- prng.seed(seed);
- } else if (std.mem.eql(u8, args[i], "--filter")) {
- i += 1;
- if (i == args.len) {
- usage();
- std.os.exit(1);
- }
-
- filter = args[i];
- } else if (std.mem.eql(u8, args[i], "--count")) {
- i += 1;
- if (i == args.len) {
- usage();
- std.os.exit(1);
- }
-
- const c = try std.fmt.parseUnsigned(u32, args[i], 10);
- count = c * MiB;
- } else if (std.mem.eql(u8, args[i], "--help")) {
- usage();
- return;
- } else {
- usage();
- std.os.exit(1);
- }
- }
-
- inline for (hashes) |H| {
- if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
- const result = try benchmarkHash(H, count);
- try printPad(stdout, H.name);
- try stdout.print(": {:4} MiB/s [{:16}]\n", result.throughput / (1 * MiB), result.hash);
- }
- }
-}
diff --git a/std/hash/wyhash.zig b/std/hash/wyhash.zig
index dfa5156cad..f5cca121f4 100644
--- a/std/hash/wyhash.zig
+++ b/std/hash/wyhash.zig
@@ -116,8 +116,8 @@ pub const Wyhash = struct {
pub fn hash(seed: u64, input: []const u8) u64 {
var c = Wyhash.init(seed);
- c.update(input);
- return c.final();
+ @inlineCall(c.update, input);
+ return @inlineCall(c.final);
}
};