diff --git a/src/terminal/hash_map.zig b/src/terminal/hash_map.zig index d8feddcd1..f1fca1670 100644 --- a/src/terminal/hash_map.zig +++ b/src/terminal/hash_map.zig @@ -75,6 +75,22 @@ fn AutoContext(comptime K: type) type { }; } +/// Hash context specialized for offset keys. Offsets are already integers, +/// so hashing them through autoHash's byte-oriented Wyhash path adds work to +/// every page-map operation. Widen before mixing so the resulting u64 also +/// has entropy in the high bits used by Metadata's fingerprint. +pub fn OffsetContext(comptime T: type) type { + return struct { + pub inline fn hash(_: @This(), key: Offset(T)) u64 { + return std.hash.int(@as(u64, key.offset)); + } + + pub inline fn eql(_: @This(), a: Offset(T), b: Offset(T)) bool { + return a.offset == b.offset; + } + }; +} + /// A HashMap type that uses offsets rather than pointers, making it /// possible to efficiently move around the backing memory without /// invalidating the HashMap. diff --git a/src/terminal/hyperlink.zig b/src/terminal/hyperlink.zig index 35a16a2ae..2a8310ec1 100644 --- a/src/terminal/hyperlink.zig +++ b/src/terminal/hyperlink.zig @@ -1,7 +1,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const hash_map = @import("hash_map.zig"); -const AutoOffsetHashMap = hash_map.AutoOffsetHashMap; +const OffsetHashMap = hash_map.OffsetHashMap; const pagepkg = @import("page.zig"); const size = @import("size.zig"); const Offset = size.Offset; @@ -20,7 +20,12 @@ pub const Id = size.HyperlinkCountInt; // The mapping of cell to hyperlink. We use an offset hash map to save space // since its very unlikely a cell is a hyperlink, so its a waste to store // the hyperlink ID in the cell itself. -pub const Map = AutoOffsetHashMap(Offset(Cell), Id, 80); +pub const Map = OffsetHashMap( + Offset(Cell), + Id, + hash_map.OffsetContext(Cell), + 80, +); /// A fully decoded hyperlink that may or may not have its /// memory within a page. The memory location of this is dependent diff --git a/src/terminal/page.zig b/src/terminal/page.zig index 653a95c44..d68a576b0 100644 --- a/src/terminal/page.zig +++ b/src/terminal/page.zig @@ -21,7 +21,7 @@ const Offset = size.Offset; const OffsetBuf = size.OffsetBuf; const BitmapAllocator = @import("bitmap_allocator.zig").BitmapAllocator; const hash_map = @import("hash_map.zig"); -const AutoOffsetHashMap = hash_map.AutoOffsetHashMap; +const OffsetHashMap = hash_map.OffsetHashMap; const alignForward = std.mem.alignForward; const alignBackward = std.mem.alignBackward; @@ -92,9 +92,10 @@ const grapheme_chunk = grapheme_chunk_len * @sizeOf(u21); const GraphemeAlloc = BitmapAllocator(grapheme_chunk); const grapheme_count_default = GraphemeAlloc.bitmap_bit_size; pub const grapheme_bytes_default = grapheme_count_default * grapheme_chunk; -const GraphemeMap = AutoOffsetHashMap( +const GraphemeMap = OffsetHashMap( Offset(Cell), Offset(u21).Slice, + hash_map.OffsetContext(Cell), hash_map.default_max_load_percentage, );