//! //! 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 << 8) | 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 Alloc = std.mem.Allocator; const value = @import("../value.zig"); const IstrPtr = value.istr.IstrPtr; 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 getIstrPtr(self: *@This()) IstrPtr { if (self.meta.len <= 24) { return @ptrCast(self); } else { return @ptrFromInt(self.bufU64()[1]); } } pub fn match(self: *@This(), hash: u64, s: []const u8) ?IstrPtr { if (self.hash != hash) { return null; } const istr = self.getIstrPtr(); if (std.hash_map.eqlString(s, istr.str())) { return istr; } else { return null; } } pub fn insert( self: *@This(), alloc: Alloc, hash: u64, s: []const u8, ) !IstrPtr { self.hash = hash; if (s.len <= 24) { const istr: IstrPtr = @ptrCast(self); istr.putStr(s); return istr; } else { const istr = try value.istr.new(alloc, hash, s); self.bufU64()[1] = @intFromPtr(istr); return istr; } } }; buckets: []Bucket = &.{}, const default_bcount = 512; pub fn init(alloc: Alloc) !Set { return initCustom(alloc, default_bcount); } pub fn initCustom(alloc: Alloc, bcount: usize) !Set { var set = Set{}; try set.realloc(alloc, bcount); return set; } fn realloc(self: *Set, alloc: Alloc, bcount: usize) !void { alloc.free(self.buckets); self.buckets = try alloc.alloc(Bucket, bcount); @memset(self.buckets, std.mem.zeroes(Bucket)); } pub fn deinit(self: *Set, alloc: Alloc) void { alloc.free(self.buckets); } fn sector(set: *Set, b_idx: usize, s_idx: usize) Sector { return @ptrCast(&set.buckets[b_idx][s_idx]); } /// Add, or get existing, string. pub fn add(self: *Set, alloc: Alloc, s: []const u8) !IstrPtr { std.debug.assert(s.len < 256); const idx_mask = self.buckets.len - 1; const h0 = std.hash_map.hashString(s); const hash = (h0 << 8) | s.len; // We resize if we had to walk through 50% of the buckets. const probe_limit = self.buckets.len >> 1; var probes: usize = 0; var idx = @as(usize, @truncate(hash & idx_mask)); const s_idx = b: while (true) : (idx = (idx + 1) & idx_mask) { inline for (0..1) |i| { const sec = sector(self, idx, i); if (sec.hash == 0) break :b i; if (sec.match(hash, s)) |istr| return istr; } probes += 1; if (probes > probe_limit) { self.resize(); return self.add(alloc, s); } }; const sec = sector(self, idx, s_idx); return sec.insert(alloc, hash, s); } fn resize(self: *Set) void { _ = self; @panic("IstrSet resize not implemented."); }