From 4a12f2b89af3b84142153fc496bd894a6a6ded03 Mon Sep 17 00:00:00 2001 From: peterino2 Date: Tue, 3 Nov 2026 10:11:09 -0800 Subject: [PATCH] sema: hash type keys as words TypeTable's key (tag, a, b) has padding, so the automatic context hashed it field by field through Wyhash's streaming interface on every vector, matrix and array type looked up. KeyContext mixes its words. Glide's shaders in the hill-climbing set: -1.1% (A/B); output unchanged on the verify corpus. --- src/sema/types.zig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/sema/types.zig b/src/sema/types.zig index 77c3201..839484a 100644 --- a/src/sema/types.zig +++ b/src/sema/types.zig @@ -535,7 +535,7 @@ pub const LinAlgMatrix = struct { comp: u32, m: u32, n: u32, use: u32, scope: u3 pub const TypeTable = struct { gpa: Allocator, types: std.ArrayList(Type) = .empty, - map: std.AutoHashMapUnmanaged(Key, TypeId) = .empty, + map: std.HashMapUnmanaged(Key, TypeId, KeyContext, std.hash_map.default_max_load_percentage) = .empty, /// scalar's results. scalars: [@typeInfo(Scalar).@"enum".field_names.len]TypeId = @splat(std.math.maxInt(TypeId)), @@ -545,6 +545,23 @@ pub const TypeTable = struct { /// Hashable identity of structural types. const Key = struct { tag: u8, a: u64, b: u64 }; + /// Hashes a Key's words (it has padding, which the automatic context + /// hashes around field by field). + const KeyContext = struct { + fn mix(x: u64) u64 { + var z = x; + z = (z ^ (z >> 30)) *% 0xbf58476d1ce4e5b9; + z = (z ^ (z >> 27)) *% 0x94d049bb133111eb; + return z ^ (z >> 31); + } + pub fn hash(_: KeyContext, k: Key) u64 { + return mix(k.a ^ mix(k.b ^ @as(u64, k.tag) << 56)); + } + pub fn eql(_: KeyContext, x: Key, y: Key) bool { + return x.tag == y.tag and x.a == y.a and x.b == y.b; + } + }; + pub fn init(gpa: Allocator) !TypeTable { var t: TypeTable = .{ .gpa = gpa }; try t.types.append(gpa, .@"error");