/// /// Efficient hash set for short(ish) strings. /// /// Layout: []Bucket /// /// * Bucket: [2]Sector /// /// * Sector: [32]u8 or [4]u64 /// /// The parser interns strings up to 255 bytes in length, using the `Istr` heap /// representation, which has an 8-byte metadata prefix. Therefore, strings of /// up to 24 bytes can fit into a 32-byte sector as an immediate Istr, so the /// pointer to the sector becomes the Istr pointer itself. /// /// The 8-byte header of an Istr is a u64 hash, but its lowest 8 bits are also /// the length of the string: u64hash = (real_u64hash << 1) | u8length /// /// Note that the empty string is never interned since strings up to 6 bytes in /// length are packed directly into a NaN value. Thus, checking if a sector is /// empty is a matter of checking its first 8 bytes as a u64 value, which would /// hold the Istr header, which cannot be zero, because the lowest 8 bits are /// the length of the string. /// /// If the length value is greater than 24, meaning the Istr can't actually fit /// here, that means the next 8 bytes of the sector are actually a pointer to /// the Istr elsewhere on the heap, and another 16 bytes are unused. /// const std = @import("std"); const Set = @This(); const Bucket = [2][4]u64; // This is really just the head of the sector, but contains functions to // manipulate the entire contents. const Sector = *align(8) packed union { hash: u64, meta: packed struct(u64) { len: u8, _: u56, }, fn bufU64(self: *@This()) [*]u64 { return @ptrCast(self); } fn getIstr(self: *@This()) Istr { if (self.meta.len <= 24) { return @ptrCast(self); } else { return @bitCast(self.bufU64()[1]); } } pub fn match(self: *@This(), hash: u64, s: []const u8) ?Istr { if (self.hash != hash) { return false; } const istr = self.getIstr(); if (std.hash_map.eqlString(s, istr.str())) { return istr; } else { return null; } } pub fn insert( self: *@This(), alloc: *Allocator, hash: u64, s: []const u8, ) Istr { self.hash = hash; if (s.len <= 24) { const istr = @ptrCast(self); istr.putStr(s); return istr; } else { const istr = Istr.new(alloc, hash, s); self.bufU64()[1] = @bitCast(istr); return istr; } } }; buckets: []Bucket, const default_bcount = 512; pub fn init(alloc: *Allocator) !Set { return initCustom(alloc, default_bcount); } pub fn initCustom(alloc: *Allocator, bcount: usize) !Set { return Set{ .buckets = try alloc.alloc(Bucket, bcount), }; } pub fn deinit(self: *Set, alloc: *Allocator) void { alloc.free(self.buckets); } pub fn add(self: *Set, s: []const u8) Istr { std.debug.assert(s.len < 256); const idx_mask = self.buckets.len - 1; const len = s.len; const h0 = std.hash_map.hashString(s); const hash = (h0 << 1) | len; // We resize if we had to walk through 50% of the buckets. const probe_limit = self.buckets.len / 2; var probes: usize = 0; var idx = @as(usize, @truncate(hash & idx_mask)); var s_idx: usize = undefined; while (true) : (idx = (idx + 1) & idx_mask) { inline for (0..1) |i| { const sec: Sector = @ptrCast(&self.buckets[idx][i]); if (sec.hash == 0) { s_idx = i; break; } if (sec.match(s)) |istr| return istr; } probes += 1; if (probes > probe_limit) { self.resize(); return self.add(s); } }; const sec = &self.buckets[idx][s_idx]; return sec.insert(alloc, hash, s); } fn resize() void { @panic("IstrSet resize not implemented."); }