authorgravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-07-02 18:40:01+02:00
committergravatar for sahnvour@pm.meSahnvour <sahnvour@pm.me> 2019-08-04 12:34:05+02:00
log5bf63bfbf113d3921101311f1e3040890b94e798
tree064720bef64e7027194531a0c0ce2f3c5ea1520f
parent8805a7b50985fca23969beab8636fbfbecd857ee

make use of hashing streaming interface in autoHash


1 files changed, 85 insertions(+), 69 deletions(-)

std/hash_map.zig+85-69
...@@ -522,8 +522,9 @@ pub fn getTrivialEqlFn(comptime K: type) (fn (K, K) bool) {...@@ -522,8 +522,9 @@ pub fn getTrivialEqlFn(comptime K: type) (fn (K, K) bool) {
522pub fn getAutoHashFn(comptime K: type) (fn (K) u32) {522pub fn getAutoHashFn(comptime K: type) (fn (K) u32) {
523 return struct {523 return struct {
524 fn hash(key: K) u32 {524 fn hash(key: K) u32 {
525 const h = autoHash(key, 0);525 var hasher = Wyhash.init(0);
526 return @truncate(u32, h);526 autoHash(&hasher, key);
527 return @truncate(u32, hasher.final());
527 }528 }
528 }.hash;529 }.hash;
529}530}
...@@ -538,10 +539,7 @@ pub fn getAutoEqlFn(comptime K: type) (fn (K, K) bool) {...@@ -538,10 +539,7 @@ pub fn getAutoEqlFn(comptime K: type) (fn (K, K) bool) {
538539
539/// Provides generic hashing for any eligible type.540/// Provides generic hashing for any eligible type.
540/// Only hashes `key` itself, pointers are not followed.541/// Only hashes `key` itself, pointers are not followed.
541/// The underlying hashing algorithm is wyhash.542pub fn autoHash(hasher: var, key: var) void {
542pub fn autoHash(key: var, seed: u64) u64 {
543 // We use the fact that wyhash takes an input seed to "chain" hasing when the
544 // key has multiple parts that are not necessarily contiguous in memory.
545 const Key = @typeOf(key);543 const Key = @typeOf(key);
546 switch (@typeInfo(Key)) {544 switch (@typeInfo(Key)) {
547 builtin.TypeId.NoReturn,545 builtin.TypeId.NoReturn,
...@@ -557,91 +555,101 @@ pub fn autoHash(key: var, seed: u64) u64 {...@@ -557,91 +555,101 @@ pub fn autoHash(key: var, seed: u64) u64 {
557 builtin.TypeId.EnumLiteral,555 builtin.TypeId.EnumLiteral,
558 => @compileError("cannot hash this type"),556 => @compileError("cannot hash this type"),
559557
560 builtin.TypeId.Int => return Wyhash.hash(seed, std.mem.asBytes(&key)),558 builtin.TypeId.Int => hasher.update(std.mem.asBytes(&key)),
561559
562 builtin.TypeId.Float => |info| return autoHash(@bitCast(@IntType(false, info.bits), key), seed),560 builtin.TypeId.Float => |info| autoHash(hasher, @bitCast(@IntType(false, info.bits), key)),
563561
564 builtin.TypeId.Bool => return autoHash(@boolToInt(key), seed),562 builtin.TypeId.Bool => autoHash(hasher, @boolToInt(key)),
565 builtin.TypeId.Enum => return autoHash(@enumToInt(key), seed),563 builtin.TypeId.Enum => autoHash(hasher, @enumToInt(key)),
566 builtin.TypeId.ErrorSet => return autoHash(@errorToInt(key), seed),564 builtin.TypeId.ErrorSet => autoHash(hasher, @errorToInt(key)),
567 builtin.TypeId.Promise, builtin.TypeId.Fn => return autoHash(@ptrToInt(key), seed),565 builtin.TypeId.Promise, builtin.TypeId.Fn => autoHash(hasher, @ptrToInt(key)),
568566
569 builtin.TypeId.Pointer => |info| return switch (info.size) {567 builtin.TypeId.Pointer => |info| switch (info.size) {
570 builtin.TypeInfo.Pointer.Size.One,568 builtin.TypeInfo.Pointer.Size.One,
571 builtin.TypeInfo.Pointer.Size.Many,569 builtin.TypeInfo.Pointer.Size.Many,
572 builtin.TypeInfo.Pointer.Size.C,570 builtin.TypeInfo.Pointer.Size.C,
573 => return autoHash(@ptrToInt(key), seed),571 => autoHash(hasher, @ptrToInt(key)),
574572
575 builtin.TypeInfo.Pointer.Size.Slice => return autoHash(key.len, autoHash(key.ptr, seed)),573 builtin.TypeInfo.Pointer.Size.Slice => {
574 autoHash(hasher, key.ptr);
575 autoHash(hasher, key.len);
576 },
576 },577 },
577578
578 builtin.TypeId.Optional => return if (key) |k| autoHash(k, seed) else 0,579 builtin.TypeId.Optional => if (key) |k| autoHash(hasher, k),
579580
580 builtin.TypeId.Array => {581 builtin.TypeId.Array => {
581 // TODO detect via a trait when Key has no padding bits to582 // TODO detect via a trait when Key has no padding bits to
582 // hash it as an array of bytes.583 // hash it as an array of bytes.
583 // Otherwise, hash every element.584 // Otherwise, hash every element.
584 var s = seed;
585 for (key) |element| {585 for (key) |element| {
586 // We reuse the hash of the previous element as the seed for the586 autoHash(hasher, element);
587 // next one so that they're dependant.
588 s = autoHash(element, s);
589 }587 }
590 return s;
591 },588 },
592589
593 builtin.TypeId.Vector => |info| {590 builtin.TypeId.Vector => |info| {
594 // If there's no unused bits in the child type, we can just hash
595 // this as an array of bytes.
596 if (info.child.bit_count % 8 == 0) {591 if (info.child.bit_count % 8 == 0) {
597 return Wyhash.hash(seed, mem.asBytes(&key));592 // If there's no unused bits in the child type, we can just hash
598 }593 // this as an array of bytes.
599594 hasher.update(mem.asBytes(&key));
600 // Otherwise, hash every element.595 } else {
601 var s = seed;596 // Otherwise, hash every element.
602 // TODO remove the copy to an array once field access is done.597 // TODO remove the copy to an array once field access is done.
603 const array: [info.len]info.child = key;598 const array: [info.len]info.child = key;
604 comptime var i: u32 = 0;599 comptime var i: u32 = 0;
605 inline while (i < info.len) : (i += 1) {600 inline while (i < info.len) : (i += 1) {
606 s = autoHash(array[i], s);601 autoHash(hasher, array[i]);
602 }
607 }603 }
608 return s;
609 },604 },
610605
611 builtin.TypeId.Struct => |info| {606 builtin.TypeId.Struct => |info| {
612 // TODO detect via a trait when Key has no padding bits to607 // TODO detect via a trait when Key has no padding bits to
613 // hash it as an array of bytes.608 // hash it as an array of bytes.
614 // Otherwise, hash every field.609 // Otherwise, hash every field.
615 var s = seed;
616 inline for (info.fields) |field| {610 inline for (info.fields) |field| {
617 // We reuse the hash of the previous field as the seed for the611 // We reuse the hash of the previous field as the seed for the
618 // next one so that they're dependant.612 // next one so that they're dependant.
619 s = autoHash(@field(key, field.name), s);613 autoHash(hasher, @field(key, field.name));
620 }614 }
621 return s;
622 },615 },
623616
624 builtin.TypeId.Union => |info| {617 builtin.TypeId.Union => |info| blk: {
625 if (info.tag_type) |tag_type| {618 if (info.tag_type) |tag_type| {
626 const tag = meta.activeTag(key);619 const tag = meta.activeTag(key);
627 const s = autoHash(tag, seed);620 const s = autoHash(hasher, tag);
628 inline for (info.fields) |field| {621 inline for (info.fields) |field| {
629 const enum_field = field.enum_field.?;622 const enum_field = field.enum_field.?;
630 if (enum_field.value == @enumToInt(tag)) {623 if (enum_field.value == @enumToInt(tag)) {
631 return autoHash(@field(key, enum_field.name), s);624 autoHash(hasher, @field(key, enum_field.name));
625 // TODO use a labelled break when it does not crash the compiler.
626 // break :blk;
627 return;
632 }628 }
633 }629 }
634 unreachable;630 unreachable;
635 } else @compileError("cannot hash untagged union type: " ++ @typeName(Key) ++ ", provide your own hash function");631 } else @compileError("cannot hash untagged union type: " ++ @typeName(Key) ++ ", provide your own hash function");
636 },632 },
637633
638 builtin.TypeId.ErrorUnion => {634 builtin.TypeId.ErrorUnion => blk: {
639 return autoHash(key catch |err| return autoHash(err, seed), seed);635 const payload = key catch |err| {
636 autoHash(hasher, err);
637 break :blk;
638 };
639 autoHash(hasher, payload);
640 },640 },
641 }641 }
642}642}
643643
644fn testAutoHash(key: var) u64 {
645 var hasher = Wyhash.init(0);
646 autoHash(&hasher, key);
647 return hasher.final();
648}
649
644test "autoHash slice" {650test "autoHash slice" {
651 // Allocate one array dynamically so that we're assured it is not merged
652 // with the other by the optimization passes.
645 const array1 = try std.heap.direct_allocator.create([6]u32);653 const array1 = try std.heap.direct_allocator.create([6]u32);
646 defer std.heap.direct_allocator.destroy(array1);654 defer std.heap.direct_allocator.destroy(array1);
647 array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 };655 array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 };
...@@ -649,38 +657,46 @@ test "autoHash slice" {...@@ -649,38 +657,46 @@ test "autoHash slice" {
649 const a = array1[0..];657 const a = array1[0..];
650 const b = array2[0..];658 const b = array2[0..];
651 const c = array1[0..3];659 const c = array1[0..3];
652 testing.expect(autoHash(a, 0) == autoHash(a, 0));660 testing.expect(testAutoHash(a) == testAutoHash(a));
653 testing.expect(autoHash(a, 0) != autoHash(array1, 0));661 testing.expect(testAutoHash(a) != testAutoHash(array1));
654 testing.expect(autoHash(a, 0) != autoHash(b, 0));662 testing.expect(testAutoHash(a) != testAutoHash(b));
655 testing.expect(autoHash(a, 0) != autoHash(c, 0));663 testing.expect(testAutoHash(a) != testAutoHash(c));
656}664}
657665
658test "autoHash optional" {666test "testAutoHash optional" {
659 const a: ?u32 = 123;667 const a: ?u32 = 123;
660 const b: ?u32 = null;668 const b: ?u32 = null;
661 testing.expectEqual(autoHash(a, 0), autoHash(u32(123), 0));669 testing.expectEqual(testAutoHash(a), testAutoHash(u32(123)));
662 testing.expect(autoHash(a, 0) != autoHash(b, 0));670 testing.expect(testAutoHash(a) != testAutoHash(b));
663 testing.expectEqual(autoHash(b, 0), 0);671 testing.expectEqual(testAutoHash(b), 0);
664}672}
665673
666test "autoHash array" {674test "testAutoHash array" {
667 const a = [_]u32{ 1, 2, 3 };675 const a = [_]u32{ 1, 2, 3 };
668 const h = autoHash(a, 0);676 const h = testAutoHash(a);
669 testing.expectEqual(h, autoHash(u32(3), autoHash(u32(2), autoHash(u32(1), 0))));677 var hasher = Wyhash.init(0);
678 autoHash(&hasher, u32(1));
679 autoHash(&hasher, u32(2));
680 autoHash(&hasher, u32(3));
681 testing.expectEqual(h, hasher.final());
670}682}
671683
672test "autoHash struct" {684test "testAutoHash struct" {
673 const Foo = struct {685 const Foo = struct {
674 a: u32 = 1,686 a: u32 = 1,
675 b: u32 = 2,687 b: u32 = 2,
676 c: u32 = 3,688 c: u32 = 3,
677 };689 };
678 const f = Foo{};690 const f = Foo{};
679 const h = autoHash(f, 0);691 const h = testAutoHash(f);
680 testing.expectEqual(h, autoHash(u32(3), autoHash(u32(2), autoHash(u32(1), 0))));692 var hasher = Wyhash.init(0);
693 autoHash(&hasher, u32(1));
694 autoHash(&hasher, u32(2));
695 autoHash(&hasher, u32(3));
696 testing.expectEqual(h, hasher.final());
681}697}
682698
683test "autoHash union" {699test "testAutoHash union" {
684 const Foo = union(enum) {700 const Foo = union(enum) {
685 A: u32,701 A: u32,
686 B: f32,702 B: f32,
...@@ -690,24 +706,24 @@ test "autoHash union" {...@@ -690,24 +706,24 @@ test "autoHash union" {
690 const a = Foo{ .A = 18 };706 const a = Foo{ .A = 18 };
691 var b = Foo{ .B = 12.34 };707 var b = Foo{ .B = 12.34 };
692 const c = Foo{ .C = 18 };708 const c = Foo{ .C = 18 };
693 testing.expect(autoHash(a, 0) == autoHash(a, 0));709 testing.expect(testAutoHash(a) == testAutoHash(a));
694 testing.expect(autoHash(a, 0) != autoHash(b, 0));710 testing.expect(testAutoHash(a) != testAutoHash(b));
695 testing.expect(autoHash(a, 0) != autoHash(c, 0));711 testing.expect(testAutoHash(a) != testAutoHash(c));
696712
697 b = Foo{ .A = 18 };713 b = Foo{ .A = 18 };
698 testing.expect(autoHash(a, 0) == autoHash(b, 0));714 testing.expect(testAutoHash(a) == testAutoHash(b));
699}715}
700716
701test "autoHash vector" {717test "testAutoHash vector" {
702 const a: @Vector(4, u32) = [_]u32{ 1, 2, 3, 4 };718 const a: @Vector(4, u32) = [_]u32{ 1, 2, 3, 4 };
703 const b: @Vector(4, u32) = [_]u32{ 1, 2, 3, 5 };719 const b: @Vector(4, u32) = [_]u32{ 1, 2, 3, 5 };
704 const c: @Vector(4, u31) = [_]u31{ 1, 2, 3, 4 };720 const c: @Vector(4, u31) = [_]u31{ 1, 2, 3, 4 };
705 testing.expect(autoHash(a, 0) == autoHash(a, 0));721 testing.expect(testAutoHash(a) == testAutoHash(a));
706 testing.expect(autoHash(a, 0) != autoHash(b, 0));722 testing.expect(testAutoHash(a) != testAutoHash(b));
707 testing.expect(autoHash(a, 0) != autoHash(c, 0));723 testing.expect(testAutoHash(a) != testAutoHash(c));
708}724}
709725
710test "autoHash error union" {726test "testAutoHash error union" {
711 const Errors = error{Test};727 const Errors = error{Test};
712 const Foo = struct {728 const Foo = struct {
713 a: u32 = 1,729 a: u32 = 1,
...@@ -716,7 +732,7 @@ test "autoHash error union" {...@@ -716,7 +732,7 @@ test "autoHash error union" {
716 };732 };
717 const f = Foo{};733 const f = Foo{};
718 const g: Errors!Foo = Errors.Test;734 const g: Errors!Foo = Errors.Test;
719 testing.expect(autoHash(f, 0) != autoHash(g, 0));735 testing.expect(testAutoHash(f) != testAutoHash(g));
720 testing.expect(autoHash(f, 0) == autoHash(Foo{}, 0));736 testing.expect(testAutoHash(f) == testAutoHash(Foo{}));
721 testing.expect(autoHash(g, 0) == autoHash(Errors.Test, 0));737 testing.expect(testAutoHash(g) == testAutoHash(Errors.Test));
722}738}