optimize hashing and equality checks on structs

This commit is contained in:
Techatrix 2023-01-17 20:36:33 +01:00
parent 2ea97a050b
commit 30f919d854

View File

@ -1061,9 +1061,9 @@ fn deepEql(a: anytype, b: @TypeOf(a)) bool {
switch (@typeInfo(T)) { switch (@typeInfo(T)) {
.Struct => |info| { .Struct => |info| {
// if (info.layout == .Packed) { if (comptime std.meta.trait.hasUniqueRepresentation(T)) {
// return std.mem.eql(u8, std.mem.asBytes(&a), std.mem.asBytes(&b)); return std.mem.eql(u8, std.mem.asBytes(&a), std.mem.asBytes(&b));
// } }
inline for (info.fields) |field_info| { inline for (info.fields) |field_info| {
if (!deepEql(@field(a, field_info.name), @field(b, field_info.name))) return false; if (!deepEql(@field(a, field_info.name), @field(b, field_info.name))) return false;
} }
@ -1147,7 +1147,7 @@ fn deepHash(hasher: anytype, key: anytype) void {
}, },
.Struct => |info| { .Struct => |info| {
if (info.layout == .Packed) { if (comptime std.meta.trait.hasUniqueRepresentation(Inner)) {
hasher.update(std.mem.asBytes(&key)); hasher.update(std.mem.asBytes(&key));
} else { } else {
inline for (info.fields) |field| { inline for (info.fields) |field| {