dxil: hash pool keys with a word mix, not streaming Wyhash
The type and constant pools hashed their small keys with Wyhash's streaming interface (init, update per field, final). A splitmix64 finalizer over the fields does (slices still go through Wyhash). The hill-climbing set: -1.1% (A/B); output unchanged on the verify corpus.
This commit is contained in:
parent
5c31ce84b8
commit
2c8f5c5f53
|
|
@ -40,26 +40,28 @@ pub const Type = union(enum) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// splitmix64's finalizer: a cheap hash of a word, for the type and
|
||||||
|
/// constant pools (a hash map uses both its high and low bits).
|
||||||
|
fn mix(x: u64) u64 {
|
||||||
|
var z = x;
|
||||||
|
z = (z ^ (z >> 30)) *% 0xbf58476d1ce4e5b9;
|
||||||
|
z = (z ^ (z >> 27)) *% 0x94d049bb133111eb;
|
||||||
|
return z ^ (z >> 31);
|
||||||
|
}
|
||||||
|
|
||||||
/// Hashes what Type.eql compares.
|
/// Hashes what Type.eql compares.
|
||||||
const TypeContext = struct {
|
const TypeContext = struct {
|
||||||
pub fn hash(_: TypeContext, t: Type) u64 {
|
pub fn hash(_: TypeContext, t: Type) u64 {
|
||||||
var h: std.hash.Wyhash = .init(@backingInt(t));
|
const tag: u64 = @backingInt(t);
|
||||||
switch (t) {
|
return switch (t) {
|
||||||
.void, .label, .metadata, .half, .float, .double => {},
|
.void, .label, .metadata, .half, .float, .double => mix(tag),
|
||||||
.int => |w| h.update(std.mem.asBytes(&w)),
|
.int => |w| mix(tag << 56 | w),
|
||||||
.pointer => |p| h.update(std.mem.asBytes(&[2]u32{ p.elem, p.addr_space })),
|
.pointer => |p| mix(tag << 56 | @as(u64, p.addr_space) << 32 | p.elem),
|
||||||
.function => |f| {
|
.function => |f| mix(tag << 56 | f.ret) ^ std.hash.Wyhash.hash(0, std.mem.sliceAsBytes(f.params)),
|
||||||
h.update(std.mem.asBytes(&f.ret));
|
.@"struct" => |st| mix(tag) ^ std.hash.Wyhash.hash(0, st.name),
|
||||||
h.update(std.mem.sliceAsBytes(f.params));
|
.array => |x| mix(mix(tag << 56 | x.elem) ^ x.len),
|
||||||
},
|
.vector => |x| mix(tag << 56 | @as(u64, x.len) << 32 | x.elem),
|
||||||
.@"struct" => |st| h.update(st.name),
|
};
|
||||||
.array => |x| {
|
|
||||||
h.update(std.mem.asBytes(&x.len));
|
|
||||||
h.update(std.mem.asBytes(&x.elem));
|
|
||||||
},
|
|
||||||
.vector => |x| h.update(std.mem.asBytes(&[2]u32{ x.len, x.elem })),
|
|
||||||
}
|
|
||||||
return h.final();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eql(_: TypeContext, a: Type, b: Type) bool {
|
pub fn eql(_: TypeContext, a: Type, b: Type) bool {
|
||||||
|
|
@ -87,18 +89,13 @@ pub const Constant = struct {
|
||||||
/// element slice, as std.meta.eql compares slices).
|
/// element slice, as std.meta.eql compares slices).
|
||||||
const ConstantContext = struct {
|
const ConstantContext = struct {
|
||||||
pub fn hash(_: ConstantContext, c: Constant) u64 {
|
pub fn hash(_: ConstantContext, c: Constant) u64 {
|
||||||
var h: std.hash.Wyhash = .init(c.ty);
|
const head = mix(@as(u64, @backingInt(c.kind)) << 32 | c.ty);
|
||||||
h.update(std.mem.asBytes(&@as(u8, @backingInt(c.kind))));
|
return switch (c.kind) {
|
||||||
switch (c.kind) {
|
.int => |v| mix(head ^ @as(u64, @bitCast(v))),
|
||||||
.int => |v| h.update(std.mem.asBytes(&v)),
|
.float_bits => |v| mix(head ^ v),
|
||||||
.float_bits => |v| h.update(std.mem.asBytes(&v)),
|
.undef, .null => head,
|
||||||
.undef, .null => {},
|
.aggregate => |a| mix(head ^ @intFromPtr(a.ptr) ^ a.len << 48),
|
||||||
.aggregate => |a| {
|
};
|
||||||
h.update(std.mem.asBytes(&@intFromPtr(a.ptr)));
|
|
||||||
h.update(std.mem.asBytes(&a.len));
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return h.final();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn eql(_: ConstantContext, a: Constant, b: Constant) bool {
|
pub fn eql(_: ConstantContext, a: Constant, b: Constant) bool {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue