| ... | ... | @@ -3,9 +3,76 @@ const builtin = @import("builtin"); |
| 3 | 3 | const mem = std.mem; |
| 4 | 4 | const meta = std.meta; |
| 5 | 5 | |
| 6 | /// Describes how pointer types should be hashed. |
| 7 | pub 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. |
| 23 | pub 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. |
| 55 | pub 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 | 73 | /// Provides generic hashing for any eligible type. |
| 7 | | /// Only hashes `key` itself, pointers are not followed. |
| 8 | | pub fn autoHash(hasher: var, key: var) void { |
| 74 | /// Strategy is provided to determine if pointers should be followed or not. |
| 75 | pub fn hash(hasher: var, key: var, comptime strat: HashStrategy) void { |
| 9 | 76 | const Key = @typeOf(key); |
| 10 | 77 | switch (@typeInfo(Key)) { |
| 11 | 78 | .NoReturn, |
| ... | ... | @@ -26,35 +93,18 @@ pub fn autoHash(hasher: var, key: var) void { |
| 26 | 93 | // TODO Check if the situation is better after #561 is resolved. |
| 27 | 94 | .Int => @inlineCall(hasher.update, std.mem.asBytes(&key)), |
| 28 | 95 | |
| 29 | | .Float => |info| autoHash(hasher, @bitCast(@IntType(false, info.bits), key)), |
| 96 | .Float => |info| hash(hasher, @bitCast(@IntType(false, info.bits), key), strat), |
| 30 | 97 | |
| 31 | | .Bool => autoHash(hasher, @boolToInt(key)), |
| 32 | | .Enum => autoHash(hasher, @enumToInt(key)), |
| 33 | | .ErrorSet => autoHash(hasher, @errorToInt(key)), |
| 34 | | .AnyFrame, .Fn => autoHash(hasher, @ptrToInt(key)), |
| 98 | .Bool => hash(hasher, @boolToInt(key), strat), |
| 99 | .Enum => hash(hasher, @enumToInt(key), strat), |
| 100 | .ErrorSet => hash(hasher, @errorToInt(key), strat), |
| 101 | .AnyFrame, .Fn => hash(hasher, @ptrToInt(key), strat), |
| 35 | 102 | |
| 36 | | .Pointer => |info| switch (info.size) { |
| 37 | | builtin.TypeInfo.Pointer.Size.One, |
| 38 | | builtin.TypeInfo.Pointer.Size.Many, |
| 39 | | builtin.TypeInfo.Pointer.Size.C, |
| 40 | | => autoHash(hasher, @ptrToInt(key)), |
| 103 | .Pointer => @inlineCall(hashPointer, hasher, key, strat), |
| 41 | 104 | |
| 42 | | builtin.TypeInfo.Pointer.Size.Slice => { |
| 43 | | autoHash(hasher, key.ptr); |
| 44 | | autoHash(hasher, key.len); |
| 45 | | }, |
| 46 | | }, |
| 105 | .Optional => if (key) |k| hash(hasher, k, strat), |
| 47 | 106 | |
| 48 | | .Optional => if (key) |k| autoHash(hasher, k), |
| 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 | | }, |
| 107 | .Array => hashArray(hasher, key, strat), |
| 58 | 108 | |
| 59 | 109 | .Vector => |info| { |
| 60 | 110 | if (info.child.bit_count % 8 == 0) { |
| ... | ... | @@ -67,7 +117,7 @@ pub fn autoHash(hasher: var, key: var) void { |
| 67 | 117 | const array: [info.len]info.child = key; |
| 68 | 118 | comptime var i: u32 = 0; |
| 69 | 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 | 129 | inline for (info.fields) |field| { |
| 80 | 130 | // We reuse the hash of the previous field as the seed for the |
| 81 | 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 | }, |
| 85 | 135 | |
| 86 | 136 | .Union => |info| blk: { |
| 87 | 137 | if (info.tag_type) |tag_type| { |
| 88 | 138 | const tag = meta.activeTag(key); |
| 89 | | const s = autoHash(hasher, tag); |
| 139 | const s = hash(hasher, tag, strat); |
| 90 | 140 | inline for (info.fields) |field| { |
| 91 | 141 | const enum_field = field.enum_field.?; |
| 92 | 142 | if (enum_field.value == @enumToInt(tag)) { |
| 93 | | autoHash(hasher, @field(key, enum_field.name)); |
| 94 | | // TODO use a labelled break when it does not crash the compiler. |
| 143 | hash(hasher, @field(key, enum_field.name), strat); |
| 144 | // TODO use a labelled break when it does not crash the compiler. cf #2908 |
| 95 | 145 | // break :blk; |
| 96 | 146 | return; |
| 97 | 147 | } |
| ... | ... | @@ -102,25 +152,77 @@ pub fn autoHash(hasher: var, key: var) void { |
| 102 | 152 | |
| 103 | 153 | .ErrorUnion => blk: { |
| 104 | 154 | const payload = key catch |err| { |
| 105 | | autoHash(hasher, err); |
| 155 | hash(hasher, err, strat); |
| 106 | 156 | break :blk; |
| 107 | 157 | }; |
| 108 | | autoHash(hasher, payload); |
| 158 | hash(hasher, payload, strat); |
| 109 | 159 | }, |
| 110 | 160 | } |
| 111 | 161 | } |
| 112 | 162 | |
| 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. |
| 166 | pub 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 | |
| 113 | 174 | const testing = std.testing; |
| 114 | 175 | const Wyhash = std.hash.Wyhash; |
| 115 | 176 | |
| 116 | | fn testAutoHash(key: var) u64 { |
| 177 | fn testHash(key: var) u64 { |
| 117 | 178 | // Any hash could be used here, for testing autoHash. |
| 118 | 179 | var hasher = Wyhash.init(0); |
| 119 | | autoHash(&hasher, key); |
| 180 | hash(&hasher, key, .Shallow); |
| 120 | 181 | return hasher.final(); |
| 121 | 182 | } |
| 122 | 183 | |
| 123 | | test "autoHash slice" { |
| 184 | fn 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 | |
| 191 | fn 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 | |
| 198 | fn 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 | |
| 205 | test "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 | |
| 225 | test "hash slice shallow" { |
| 124 | 226 | // Allocate one array dynamically so that we're assured it is not merged |
| 125 | 227 | // with the other by the optimization passes. |
| 126 | 228 | const array1 = try std.heap.direct_allocator.create([6]u32); |
| ... | ... | @@ -130,23 +232,78 @@ test "autoHash slice" { |
| 130 | 232 | const a = array1[0..]; |
| 131 | 233 | const b = array2[0..]; |
| 132 | 234 | const c = array1[0..3]; |
| 133 | | testing.expect(testAutoHash(a) == testAutoHash(a)); |
| 134 | | testing.expect(testAutoHash(a) != testAutoHash(array1)); |
| 135 | | testing.expect(testAutoHash(a) != testAutoHash(b)); |
| 136 | | testing.expect(testAutoHash(a) != testAutoHash(c)); |
| 235 | testing.expect(testHashShallow(a) == testHashShallow(a)); |
| 236 | testing.expect(testHashShallow(a) != testHashShallow(array1)); |
| 237 | testing.expect(testHashShallow(a) != testHashShallow(b)); |
| 238 | testing.expect(testHashShallow(a) != testHashShallow(c)); |
| 239 | } |
| 240 | |
| 241 | test "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 | |
| 257 | test "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 | } |
| 138 | 295 | |
| 139 | | test "testAutoHash optional" { |
| 296 | test "testHash optional" { |
| 140 | 297 | const a: ?u32 = 123; |
| 141 | 298 | const b: ?u32 = null; |
| 142 | | testing.expectEqual(testAutoHash(a), testAutoHash(u32(123))); |
| 143 | | testing.expect(testAutoHash(a) != testAutoHash(b)); |
| 144 | | testing.expectEqual(testAutoHash(b), 0); |
| 299 | testing.expectEqual(testHash(a), testHash(u32(123))); |
| 300 | testing.expect(testHash(a) != testHash(b)); |
| 301 | testing.expectEqual(testHash(b), 0); |
| 145 | 302 | } |
| 146 | 303 | |
| 147 | | test "testAutoHash array" { |
| 304 | test "testHash array" { |
| 148 | 305 | const a = [_]u32{ 1, 2, 3 }; |
| 149 | | const h = testAutoHash(a); |
| 306 | const h = testHash(a); |
| 150 | 307 | var hasher = Wyhash.init(0); |
| 151 | 308 | autoHash(&hasher, u32(1)); |
| 152 | 309 | autoHash(&hasher, u32(2)); |
| ... | ... | @@ -154,14 +311,14 @@ test "testAutoHash array" { |
| 154 | 311 | testing.expectEqual(h, hasher.final()); |
| 155 | 312 | } |
| 156 | 313 | |
| 157 | | test "testAutoHash struct" { |
| 314 | test "testHash struct" { |
| 158 | 315 | const Foo = struct { |
| 159 | 316 | a: u32 = 1, |
| 160 | 317 | b: u32 = 2, |
| 161 | 318 | c: u32 = 3, |
| 162 | 319 | }; |
| 163 | 320 | const f = Foo{}; |
| 164 | | const h = testAutoHash(f); |
| 321 | const h = testHash(f); |
| 165 | 322 | var hasher = Wyhash.init(0); |
| 166 | 323 | autoHash(&hasher, u32(1)); |
| 167 | 324 | autoHash(&hasher, u32(2)); |
| ... | ... | @@ -169,7 +326,7 @@ test "testAutoHash struct" { |
| 169 | 326 | testing.expectEqual(h, hasher.final()); |
| 170 | 327 | } |
| 171 | 328 | |
| 172 | | test "testAutoHash union" { |
| 329 | test "testHash union" { |
| 173 | 330 | const Foo = union(enum) { |
| 174 | 331 | A: u32, |
| 175 | 332 | B: f32, |
| ... | ... | @@ -179,24 +336,24 @@ test "testAutoHash union" { |
| 179 | 336 | const a = Foo{ .A = 18 }; |
| 180 | 337 | var b = Foo{ .B = 12.34 }; |
| 181 | 338 | const c = Foo{ .C = 18 }; |
| 182 | | testing.expect(testAutoHash(a) == testAutoHash(a)); |
| 183 | | testing.expect(testAutoHash(a) != testAutoHash(b)); |
| 184 | | testing.expect(testAutoHash(a) != testAutoHash(c)); |
| 339 | testing.expect(testHash(a) == testHash(a)); |
| 340 | testing.expect(testHash(a) != testHash(b)); |
| 341 | testing.expect(testHash(a) != testHash(c)); |
| 185 | 342 | |
| 186 | 343 | b = Foo{ .A = 18 }; |
| 187 | | testing.expect(testAutoHash(a) == testAutoHash(b)); |
| 344 | testing.expect(testHash(a) == testHash(b)); |
| 188 | 345 | } |
| 189 | 346 | |
| 190 | | test "testAutoHash vector" { |
| 347 | test "testHash vector" { |
| 191 | 348 | const a: @Vector(4, u32) = [_]u32{ 1, 2, 3, 4 }; |
| 192 | 349 | const b: @Vector(4, u32) = [_]u32{ 1, 2, 3, 5 }; |
| 193 | 350 | const c: @Vector(4, u31) = [_]u31{ 1, 2, 3, 4 }; |
| 194 | | testing.expect(testAutoHash(a) == testAutoHash(a)); |
| 195 | | testing.expect(testAutoHash(a) != testAutoHash(b)); |
| 196 | | testing.expect(testAutoHash(a) != testAutoHash(c)); |
| 351 | testing.expect(testHash(a) == testHash(a)); |
| 352 | testing.expect(testHash(a) != testHash(b)); |
| 353 | testing.expect(testHash(a) != testHash(c)); |
| 197 | 354 | } |
| 198 | 355 | |
| 199 | | test "testAutoHash error union" { |
| 356 | test "testHash error union" { |
| 200 | 357 | const Errors = error{Test}; |
| 201 | 358 | const Foo = struct { |
| 202 | 359 | a: u32 = 1, |
| ... | ... | @@ -205,7 +362,7 @@ test "testAutoHash error union" { |
| 205 | 362 | }; |
| 206 | 363 | const f = Foo{}; |
| 207 | 364 | const g: Errors!Foo = Errors.Test; |
| 208 | | testing.expect(testAutoHash(f) != testAutoHash(g)); |
| 209 | | testing.expect(testAutoHash(f) == testAutoHash(Foo{})); |
| 210 | | testing.expect(testAutoHash(g) == testAutoHash(Errors.Test)); |
| 365 | testing.expect(testHash(f) != testHash(g)); |
| 366 | testing.expect(testHash(f) == testHash(Foo{})); |
| 367 | testing.expect(testHash(g) == testHash(Errors.Test)); |
| 211 | 368 | } |