authorgravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-08-13 21:14:21+02:00
committergravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-08-24 15:30:23+02:00
log1498ccac2a7c4370ae09cd69a0f2ade7758fcd64
tree43197d7366ea1f9ce2c1fa479faa3315e1099b74
parent50a80261dcc1471911a63b9121096b80edbf6ff0

auto_hash: better generic hashing implementation

autoHash forbids slices as input hash was added to handle all types from autoHash plus slices, with a specified strategy

1 files changed, 219 insertions(+), 62 deletions(-)

std/hash/auto_hash.zig+219-62
...@@ -3,9 +3,76 @@ const builtin = @import("builtin");...@@ -3,9 +3,76 @@ const builtin = @import("builtin");
3const mem = std.mem;3const mem = std.mem;
4const meta = std.meta;4const meta = std.meta;
55
6/// Describes how pointer types should be hashed.
7pub const HashStrategy = enum {
8 /// Do not follow pointers, only hash their value.
9 Shallow,
10
11 /// Follow pointers, hash the pointee content.
12 /// Only dereferences one level, ie. it is changed into .Shallow when a
13 /// pointer type is encountered.
14 Deep,
15
16 /// Follow pointers, hash the pointee content.
17 /// Dereferences all pointers encountered.
18 /// Assumes no cycle.
19 DeepRecursive,
20};
21
22/// Helper function to hash a pointer and mutate the strategy if needed.
23pub fn hashPointer(hasher: var, key: var, comptime strat: HashStrategy) void {
24 const info = @typeInfo(@typeOf(key));
25
26 switch (info.Pointer.size) {
27 builtin.TypeInfo.Pointer.Size.One => switch (strat) {
28 .Shallow => hash(hasher, @ptrToInt(key), .Shallow),
29 .Deep => hash(hasher, key.*, .Shallow),
30 .DeepRecursive => hash(hasher, key.*, .DeepRecursive),
31 },
32
33 builtin.TypeInfo.Pointer.Size.Slice => switch (strat) {
34 .Shallow => {
35 hashPointer(hasher, key.ptr, .Shallow);
36 hash(hasher, key.len, .Shallow);
37 },
38 .Deep => hashArray(hasher, key, .Shallow),
39 .DeepRecursive => hashArray(hasher, key, .DeepRecursive),
40 },
41
42 builtin.TypeInfo.Pointer.Size.Many,
43 builtin.TypeInfo.Pointer.Size.C,
44 => switch (strat) {
45 .Shallow => hash(hasher, @ptrToInt(key), .Shallow),
46 else => @compileError(
47 \\ unknown-length pointers and C pointers cannot be hashed deeply.
48 \\ Consider providing your own hash function.
49 ),
50 },
51 }
52}
53
54/// Helper function to hash a set of contiguous objects, from an array or slice.
55pub fn hashArray(hasher: var, key: var, comptime strat: HashStrategy) void {
56 switch (strat) {
57 .Shallow => {
58 // TODO detect via a trait when Key has no padding bits to
59 // hash it as an array of bytes.
60 // Otherwise, hash every element.
61 for (key) |element| {
62 hash(hasher, element, .Shallow);
63 }
64 },
65 else => {
66 for (key) |element| {
67 hash(hasher, element, strat);
68 }
69 },
70 }
71}
72
6/// Provides generic hashing for any eligible type.73/// Provides generic hashing for any eligible type.
7/// Only hashes `key` itself, pointers are not followed.74/// Strategy is provided to determine if pointers should be followed or not.
8pub fn autoHash(hasher: var, key: var) void {75pub fn hash(hasher: var, key: var, comptime strat: HashStrategy) void {
9 const Key = @typeOf(key);76 const Key = @typeOf(key);
10 switch (@typeInfo(Key)) {77 switch (@typeInfo(Key)) {
11 .NoReturn,78 .NoReturn,
...@@ -26,35 +93,18 @@ pub fn autoHash(hasher: var, key: var) void {...@@ -26,35 +93,18 @@ pub fn autoHash(hasher: var, key: var) void {
26 // TODO Check if the situation is better after #561 is resolved.93 // TODO Check if the situation is better after #561 is resolved.
27 .Int => @inlineCall(hasher.update, std.mem.asBytes(&key)),94 .Int => @inlineCall(hasher.update, std.mem.asBytes(&key)),
2895
29 .Float => |info| autoHash(hasher, @bitCast(@IntType(false, info.bits), key)),96 .Float => |info| hash(hasher, @bitCast(@IntType(false, info.bits), key), strat),
3097
31 .Bool => autoHash(hasher, @boolToInt(key)),98 .Bool => hash(hasher, @boolToInt(key), strat),
32 .Enum => autoHash(hasher, @enumToInt(key)),99 .Enum => hash(hasher, @enumToInt(key), strat),
33 .ErrorSet => autoHash(hasher, @errorToInt(key)),100 .ErrorSet => hash(hasher, @errorToInt(key), strat),
34 .AnyFrame, .Fn => autoHash(hasher, @ptrToInt(key)),101 .AnyFrame, .Fn => hash(hasher, @ptrToInt(key), strat),
35102
36 .Pointer => |info| switch (info.size) {103 .Pointer => @inlineCall(hashPointer, hasher, key, strat),
37 builtin.TypeInfo.Pointer.Size.One,
38 builtin.TypeInfo.Pointer.Size.Many,
39 builtin.TypeInfo.Pointer.Size.C,
40 => autoHash(hasher, @ptrToInt(key)),
41104
42 builtin.TypeInfo.Pointer.Size.Slice => {105 .Optional => if (key) |k| hash(hasher, k, strat),
43 autoHash(hasher, key.ptr);
44 autoHash(hasher, key.len);
45 },
46 },
47106
48 .Optional => if (key) |k| autoHash(hasher, k),107 .Array => hashArray(hasher, key, strat),
49
50 .Array => {
51 // TODO detect via a trait when Key has no padding bits to
52 // hash it as an array of bytes.
53 // Otherwise, hash every element.
54 for (key) |element| {
55 autoHash(hasher, element);
56 }
57 },
58108
59 .Vector => |info| {109 .Vector => |info| {
60 if (info.child.bit_count % 8 == 0) {110 if (info.child.bit_count % 8 == 0) {
...@@ -67,7 +117,7 @@ pub fn autoHash(hasher: var, key: var) void {...@@ -67,7 +117,7 @@ pub fn autoHash(hasher: var, key: var) void {
67 const array: [info.len]info.child = key;117 const array: [info.len]info.child = key;
68 comptime var i: u32 = 0;118 comptime var i: u32 = 0;
69 inline while (i < info.len) : (i += 1) {119 inline while (i < info.len) : (i += 1) {
70 autoHash(hasher, array[i]);120 hash(hasher, array[i], strat);
71 }121 }
72 }122 }
73 },123 },
...@@ -79,19 +129,19 @@ pub fn autoHash(hasher: var, key: var) void {...@@ -79,19 +129,19 @@ pub fn autoHash(hasher: var, key: var) void {
79 inline for (info.fields) |field| {129 inline for (info.fields) |field| {
80 // We reuse the hash of the previous field as the seed for the130 // We reuse the hash of the previous field as the seed for the
81 // next one so that they're dependant.131 // next one so that they're dependant.
82 autoHash(hasher, @field(key, field.name));132 hash(hasher, @field(key, field.name), strat);
83 }133 }
84 },134 },
85135
86 .Union => |info| blk: {136 .Union => |info| blk: {
87 if (info.tag_type) |tag_type| {137 if (info.tag_type) |tag_type| {
88 const tag = meta.activeTag(key);138 const tag = meta.activeTag(key);
89 const s = autoHash(hasher, tag);139 const s = hash(hasher, tag, strat);
90 inline for (info.fields) |field| {140 inline for (info.fields) |field| {
91 const enum_field = field.enum_field.?;141 const enum_field = field.enum_field.?;
92 if (enum_field.value == @enumToInt(tag)) {142 if (enum_field.value == @enumToInt(tag)) {
93 autoHash(hasher, @field(key, enum_field.name));143 hash(hasher, @field(key, enum_field.name), strat);
94 // TODO use a labelled break when it does not crash the compiler.144 // TODO use a labelled break when it does not crash the compiler. cf #2908
95 // break :blk;145 // break :blk;
96 return;146 return;
97 }147 }
...@@ -102,25 +152,77 @@ pub fn autoHash(hasher: var, key: var) void {...@@ -102,25 +152,77 @@ pub fn autoHash(hasher: var, key: var) void {
102152
103 .ErrorUnion => blk: {153 .ErrorUnion => blk: {
104 const payload = key catch |err| {154 const payload = key catch |err| {
105 autoHash(hasher, err);155 hash(hasher, err, strat);
106 break :blk;156 break :blk;
107 };157 };
108 autoHash(hasher, payload);158 hash(hasher, payload, strat);
109 },159 },
110 }160 }
111}161}
112162
163/// Provides generic hashing for any eligible type.
164/// Only hashes `key` itself, pointers are not followed.
165/// Slices are rejected to avoid ambiguity on the user's intention.
166pub fn autoHash(hasher: var, key: var) void {
167 const Key = @typeOf(key);
168 if (comptime meta.trait.isSlice(Key))
169 @compileError("std.auto_hash.autoHash does not allow slices (here " ++ @typeName(Key) ++ " because the intent is unclear. Consider using std.auto_hash.hash or providing your own hash function instead.");
170
171 hash(hasher, key, .Shallow);
172}
173
113const testing = std.testing;174const testing = std.testing;
114const Wyhash = std.hash.Wyhash;175const Wyhash = std.hash.Wyhash;
115176
116fn testAutoHash(key: var) u64 {177fn testHash(key: var) u64 {
117 // Any hash could be used here, for testing autoHash.178 // Any hash could be used here, for testing autoHash.
118 var hasher = Wyhash.init(0);179 var hasher = Wyhash.init(0);
119 autoHash(&hasher, key);180 hash(&hasher, key, .Shallow);
120 return hasher.final();181 return hasher.final();
121}182}
122183
123test "autoHash slice" {184fn testHashShallow(key: var) u64 {
185 // Any hash could be used here, for testing autoHash.
186 var hasher = Wyhash.init(0);
187 hash(&hasher, key, .Shallow);
188 return hasher.final();
189}
190
191fn testHashDeep(key: var) u64 {
192 // Any hash could be used here, for testing autoHash.
193 var hasher = Wyhash.init(0);
194 hash(&hasher, key, .Deep);
195 return hasher.final();
196}
197
198fn testHashDeepRecursive(key: var) u64 {
199 // Any hash could be used here, for testing autoHash.
200 var hasher = Wyhash.init(0);
201 hash(&hasher, key, .DeepRecursive);
202 return hasher.final();
203}
204
205test "hash pointer" {
206 const array = [_]u32{ 123, 123, 123 };
207 const a = &array[0];
208 const b = &array[1];
209 const c = &array[2];
210 const d = a;
211
212 testing.expect(testHashShallow(a) == testHashShallow(d));
213 testing.expect(testHashShallow(a) != testHashShallow(c));
214 testing.expect(testHashShallow(a) != testHashShallow(b));
215
216 testing.expect(testHashDeep(a) == testHashDeep(a));
217 testing.expect(testHashDeep(a) == testHashDeep(c));
218 testing.expect(testHashDeep(a) == testHashDeep(b));
219
220 testing.expect(testHashDeepRecursive(a) == testHashDeepRecursive(a));
221 testing.expect(testHashDeepRecursive(a) == testHashDeepRecursive(c));
222 testing.expect(testHashDeepRecursive(a) == testHashDeepRecursive(b));
223}
224
225test "hash slice shallow" {
124 // Allocate one array dynamically so that we're assured it is not merged226 // Allocate one array dynamically so that we're assured it is not merged
125 // with the other by the optimization passes.227 // with the other by the optimization passes.
126 const array1 = try std.heap.direct_allocator.create([6]u32);228 const array1 = try std.heap.direct_allocator.create([6]u32);
...@@ -130,23 +232,78 @@ test "autoHash slice" {...@@ -130,23 +232,78 @@ test "autoHash slice" {
130 const a = array1[0..];232 const a = array1[0..];
131 const b = array2[0..];233 const b = array2[0..];
132 const c = array1[0..3];234 const c = array1[0..3];
133 testing.expect(testAutoHash(a) == testAutoHash(a));235 testing.expect(testHashShallow(a) == testHashShallow(a));
134 testing.expect(testAutoHash(a) != testAutoHash(array1));236 testing.expect(testHashShallow(a) != testHashShallow(array1));
135 testing.expect(testAutoHash(a) != testAutoHash(b));237 testing.expect(testHashShallow(a) != testHashShallow(b));
136 testing.expect(testAutoHash(a) != testAutoHash(c));238 testing.expect(testHashShallow(a) != testHashShallow(c));
239}
240
241test "hash slice deep" {
242 // Allocate one array dynamically so that we're assured it is not merged
243 // with the other by the optimization passes.
244 const array1 = try std.heap.direct_allocator.create([6]u32);
245 defer std.heap.direct_allocator.destroy(array1);
246 array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 };
247 const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 };
248 const a = array1[0..];
249 const b = array2[0..];
250 const c = array1[0..3];
251 testing.expect(testHashDeep(a) == testHashDeep(a));
252 testing.expect(testHashDeep(a) == testHashDeep(array1));
253 testing.expect(testHashDeep(a) == testHashDeep(b));
254 testing.expect(testHashDeep(a) != testHashDeep(c));
255}
256
257test "hash struct deep" {
258 const Foo = struct {
259 a: u32,
260 b: f64,
261 c: *bool,
262
263 const Self = @This();
264
265 pub fn init(allocator: *mem.Allocator, a_: u32, b_: f64, c_: bool) !Self {
266 const ptr = try allocator.create(bool);
267 ptr.* = c_;
268 return Self{ .a = a_, .b = b_, .c = ptr };
269 }
270 };
271
272 const allocator = std.heap.direct_allocator;
273 const foo = try Foo.init(allocator, 123, 1.0, true);
274 const bar = try Foo.init(allocator, 123, 1.0, true);
275 const baz = try Foo.init(allocator, 123, 1.0, false);
276 defer allocator.destroy(foo.c);
277 defer allocator.destroy(bar.c);
278 defer allocator.destroy(baz.c);
279
280 testing.expect(testHashDeep(foo) == testHashDeep(bar));
281 testing.expect(testHashDeep(foo) != testHashDeep(baz));
282 testing.expect(testHashDeep(bar) != testHashDeep(baz));
283
284 var hasher = Wyhash.init(0);
285 const h = testHashDeep(foo);
286 autoHash(&hasher, foo.a);
287 autoHash(&hasher, foo.b);
288 autoHash(&hasher, foo.c.*);
289 testing.expectEqual(h, hasher.final());
290
291 const h2 = testHashDeepRecursive(&foo);
292 testing.expect(h2 != testHashDeep(&foo));
293 testing.expect(h2 == testHashDeep(foo));
137}294}
138295
139test "testAutoHash optional" {296test "testHash optional" {
140 const a: ?u32 = 123;297 const a: ?u32 = 123;
141 const b: ?u32 = null;298 const b: ?u32 = null;
142 testing.expectEqual(testAutoHash(a), testAutoHash(u32(123)));299 testing.expectEqual(testHash(a), testHash(u32(123)));
143 testing.expect(testAutoHash(a) != testAutoHash(b));300 testing.expect(testHash(a) != testHash(b));
144 testing.expectEqual(testAutoHash(b), 0);301 testing.expectEqual(testHash(b), 0);
145}302}
146303
147test "testAutoHash array" {304test "testHash array" {
148 const a = [_]u32{ 1, 2, 3 };305 const a = [_]u32{ 1, 2, 3 };
149 const h = testAutoHash(a);306 const h = testHash(a);
150 var hasher = Wyhash.init(0);307 var hasher = Wyhash.init(0);
151 autoHash(&hasher, u32(1));308 autoHash(&hasher, u32(1));
152 autoHash(&hasher, u32(2));309 autoHash(&hasher, u32(2));
...@@ -154,14 +311,14 @@ test "testAutoHash array" {...@@ -154,14 +311,14 @@ test "testAutoHash array" {
154 testing.expectEqual(h, hasher.final());311 testing.expectEqual(h, hasher.final());
155}312}
156313
157test "testAutoHash struct" {314test "testHash struct" {
158 const Foo = struct {315 const Foo = struct {
159 a: u32 = 1,316 a: u32 = 1,
160 b: u32 = 2,317 b: u32 = 2,
161 c: u32 = 3,318 c: u32 = 3,
162 };319 };
163 const f = Foo{};320 const f = Foo{};
164 const h = testAutoHash(f);321 const h = testHash(f);
165 var hasher = Wyhash.init(0);322 var hasher = Wyhash.init(0);
166 autoHash(&hasher, u32(1));323 autoHash(&hasher, u32(1));
167 autoHash(&hasher, u32(2));324 autoHash(&hasher, u32(2));
...@@ -169,7 +326,7 @@ test "testAutoHash struct" {...@@ -169,7 +326,7 @@ test "testAutoHash struct" {
169 testing.expectEqual(h, hasher.final());326 testing.expectEqual(h, hasher.final());
170}327}
171328
172test "testAutoHash union" {329test "testHash union" {
173 const Foo = union(enum) {330 const Foo = union(enum) {
174 A: u32,331 A: u32,
175 B: f32,332 B: f32,
...@@ -179,24 +336,24 @@ test "testAutoHash union" {...@@ -179,24 +336,24 @@ test "testAutoHash union" {
179 const a = Foo{ .A = 18 };336 const a = Foo{ .A = 18 };
180 var b = Foo{ .B = 12.34 };337 var b = Foo{ .B = 12.34 };
181 const c = Foo{ .C = 18 };338 const c = Foo{ .C = 18 };
182 testing.expect(testAutoHash(a) == testAutoHash(a));339 testing.expect(testHash(a) == testHash(a));
183 testing.expect(testAutoHash(a) != testAutoHash(b));340 testing.expect(testHash(a) != testHash(b));
184 testing.expect(testAutoHash(a) != testAutoHash(c));341 testing.expect(testHash(a) != testHash(c));
185342
186 b = Foo{ .A = 18 };343 b = Foo{ .A = 18 };
187 testing.expect(testAutoHash(a) == testAutoHash(b));344 testing.expect(testHash(a) == testHash(b));
188}345}
189346
190test "testAutoHash vector" {347test "testHash vector" {
191 const a: @Vector(4, u32) = [_]u32{ 1, 2, 3, 4 };348 const a: @Vector(4, u32) = [_]u32{ 1, 2, 3, 4 };
192 const b: @Vector(4, u32) = [_]u32{ 1, 2, 3, 5 };349 const b: @Vector(4, u32) = [_]u32{ 1, 2, 3, 5 };
193 const c: @Vector(4, u31) = [_]u31{ 1, 2, 3, 4 };350 const c: @Vector(4, u31) = [_]u31{ 1, 2, 3, 4 };
194 testing.expect(testAutoHash(a) == testAutoHash(a));351 testing.expect(testHash(a) == testHash(a));
195 testing.expect(testAutoHash(a) != testAutoHash(b));352 testing.expect(testHash(a) != testHash(b));
196 testing.expect(testAutoHash(a) != testAutoHash(c));353 testing.expect(testHash(a) != testHash(c));
197}354}
198355
199test "testAutoHash error union" {356test "testHash error union" {
200 const Errors = error{Test};357 const Errors = error{Test};
201 const Foo = struct {358 const Foo = struct {
202 a: u32 = 1,359 a: u32 = 1,
...@@ -205,7 +362,7 @@ test "testAutoHash error union" {...@@ -205,7 +362,7 @@ test "testAutoHash error union" {
205 };362 };
206 const f = Foo{};363 const f = Foo{};
207 const g: Errors!Foo = Errors.Test;364 const g: Errors!Foo = Errors.Test;
208 testing.expect(testAutoHash(f) != testAutoHash(g));365 testing.expect(testHash(f) != testHash(g));
209 testing.expect(testAutoHash(f) == testAutoHash(Foo{}));366 testing.expect(testHash(f) == testHash(Foo{}));
210 testing.expect(testAutoHash(g) == testAutoHash(Errors.Test));367 testing.expect(testHash(g) == testHash(Errors.Test));
211}368}