Micro-optimize GlyphKey Context (#8536)

Use fast hash function on key for better distribution.

Direct compare glyph in eql to avoid Packed.from() if not neccessary.

16% -> 6.4% reduction during profiling runs.
pull/8542/head
Mitchell Hashimoto 2025-09-05 15:25:14 -07:00 committed by GitHub
commit e4c3a56242
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 2 deletions

View File

@ -332,11 +332,16 @@ const GlyphKey = struct {
const Context = struct {
pub fn hash(_: Context, key: GlyphKey) u64 {
return @bitCast(Packed.from(key));
// Packed is a u64 but std.hash.int improves uniformity and
// avoids collisions in our hashmap.
const packed_key = Packed.from(key);
return std.hash.int(@as(u64, @bitCast(packed_key)));
}
pub fn eql(_: Context, a: GlyphKey, b: GlyphKey) bool {
return Packed.from(a) == Packed.from(b);
// Packed checks glyphs but in most cases the glyphs are NOT
// equal so the first check leads to increased throughput.
return a.glyph == b.glyph and Packed.from(a) == Packed.from(b);
}
};