perf: improve style hash and eql fns

pull/9645/head
Qwerasd 2025-11-17 21:32:48 -07:00
parent 9e754f9939
commit 58c26957b4
1 changed files with 23 additions and 23 deletions

View File

@ -54,6 +54,15 @@ pub const Style = struct {
rgb, rgb,
}; };
/// True if the color is equal to another color.
pub fn eql(self: Color, other: Color) bool {
return @as(Tag, self) == @as(Tag, other) and switch (self) {
.none => true,
.palette => self.palette == other.palette,
.rgb => self.rgb == other.rgb,
};
}
/// Formatting to make debug logs easier to read /// Formatting to make debug logs easier to read
/// by only including non-default attributes. /// by only including non-default attributes.
pub fn format( pub fn format(
@ -79,28 +88,16 @@ pub const Style = struct {
}; };
/// True if the style is the default style. /// True if the style is the default style.
pub fn default(self: Style) bool { pub inline fn default(self: Style) bool {
return self.eql(.{}); return self.eql(.{});
} }
/// True if the style is equal to another style. /// True if the style is equal to another style.
/// For performance do direct comparisons first.
pub fn eql(self: Style, other: Style) bool { pub fn eql(self: Style, other: Style) bool {
inline for (comptime std.meta.fields(Style)) |field| { return self.flags == other.flags and
if (comptime std.meta.hasUniqueRepresentation(field.type)) { self.fg_color.eql(other.fg_color) and
if (@field(self, field.name) != @field(other, field.name)) { self.bg_color.eql(other.bg_color) and
return false; self.underline_color.eql(other.underline_color);
}
}
}
inline for (comptime std.meta.fields(Style)) |field| {
if (comptime !std.meta.hasUniqueRepresentation(field.type)) {
if (!std.meta.eql(@field(self, field.name), @field(other, field.name))) {
return false;
}
}
}
return true;
} }
/// Returns the bg color for a cell with this style given the cell /// Returns the bg color for a cell with this style given the cell
@ -509,12 +506,12 @@ pub const Style = struct {
} }
}; };
fn fromStyle(style: Style) PackedStyle { inline fn fromStyle(style: Style) PackedStyle {
return .{ return .{
.tags = .{ .tags = .{
.fg = std.meta.activeTag(style.fg_color), .fg = @as(Color.Tag, style.fg_color),
.bg = std.meta.activeTag(style.bg_color), .bg = @as(Color.Tag, style.bg_color),
.underline = std.meta.activeTag(style.underline_color), .underline = @as(Color.Tag, style.underline_color),
}, },
.data = .{ .data = .{
.fg = .fromColor(style.fg_color), .fg = .fromColor(style.fg_color),
@ -527,8 +524,11 @@ pub const Style = struct {
}; };
pub fn hash(self: *const Style) u64 { pub fn hash(self: *const Style) u64 {
const packed_style = PackedStyle.fromStyle(self.*); // We pack the style in to 128 bits, fold it to 64 bits,
return std.hash.XxHash3.hash(0, std.mem.asBytes(&packed_style)); // then use std.hash.int to make it sufficiently uniform.
const packed_style: PackedStyle = .fromStyle(self.*);
const wide: [2]u64 = @bitCast(packed_style);
return @call(.always_inline, std.hash.int, .{wide[0] ^ wide[1]});
} }
comptime { comptime {