//! //! 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_sectors = 2; const sector_u64_cnt = 4; const sector_size = sector_u64_cnt * 8; const Bucket = [bucket_sectors][sector_u64_cnt]u64; const SectorPtr = *align(8) SectorHead; const SectorHead = packed union { hash: u64, meta: packed struct(u64) { len: u8, _: u56, }, fn bufU64(self: SectorPtr) [*]u64 { return @ptrCast(self); } fn getIstrPtr(self: SectorPtr) IstrPtr { if (self.meta.len <= sector_size - @sizeOf(SectorHead)) { return @ptrCast(self); } else { return @ptrFromInt(self.bufU64()[1]); } } pub fn match(self: SectorPtr, 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: SectorPtr, alloc: Alloc, hash: u64, s: []const u8, ) !IstrPtr { self.hash = hash; if (s.len <= sector_size - @sizeOf(SectorHead)) { 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; } } pub fn copy(self: SectorPtr, dest: SectorPtr) void { const end = sector_u64_cnt; @memcpy(dest.bufU64()[0..end], self.bufU64()[0..end]); } }; buckets: []Bucket, used_sectors: usize = 0, const default_bcount = 512; pub fn init(alloc: Alloc) !Set { return initCustom(alloc, default_bcount); } pub fn initCustom(alloc: Alloc, bcount: usize) !Set { std.debug.assert(@popCount(bcount) == 1); // Must be power of 2. return Set{ .buckets = try allocBuckets(alloc, bcount) }; } fn allocBuckets(alloc: Alloc, bcount: usize) ![]Bucket { const bs = try alloc.alloc(Bucket, bcount); @memset(bs, std.mem.zeroes(Bucket)); return bs; } pub fn deinit(self: *Set, alloc: Alloc) void { alloc.free(self.buckets); } fn sector(bs: []Bucket, b_idx: usize, s_idx: usize) SectorPtr { return @ptrCast(&bs[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 real_hash = std.hash_map.hashString(s); const hash = (real_hash << 8) | s.len; const idx_init = @as(usize, @truncate(hash & idx_mask)); var idx = idx_init; const s_idx = b: while (true) : (idx = (idx + 1) & idx_mask) { inline for (0..bucket_sectors) |si| { const sec = sector(self.buckets, idx, si); if (sec.hash == 0) break :b si; if (sec.match(hash, s)) |istr| return istr; } }; // Check if we walked 64 buckets (4096 bytes) if ((idx -% idx_init) >= 64) { try self.resize(alloc); return self.add(alloc, s); } const sec = sector(self.buckets, idx, s_idx); const result = sec.insert(alloc, hash, s); self.used_sectors += 1; // Resize at 80% fill rate if (self.used_sectors >= (self.buckets.len * bucket_sectors * 8) / 10) { try self.resize(alloc); } return result; } fn resize(self: *Set, alloc: Alloc) !void { const old_bc = self.buckets.len; const old_bs = self.buckets; const new_bc = old_bc << 1; const new_bs = try allocBuckets(alloc, new_bc); @memset(new_bs, std.mem.zeroes(Bucket)); // Find a cluster root // const idx_init = for (old_bs, 0..) |_, idx| { // if (sector(old_bs, idx, 0).hash == 0) break idx; // } const idx_mask = new_bc - 1; for (old_bs, 0..) |_, idx| { inline for (0..bucket_sectors) |s_idx| { const sec = sector(old_bs, idx, s_idx); if (sec.hash != 0) transfer(sec, new_bs, idx_mask); } } alloc.free(old_bs); self.buckets = new_bs; } fn transfer(sec: SectorPtr, new_bs: []Bucket, idx_mask: usize) void { var new_idx = @as(usize, @truncate(sec.hash & idx_mask)); while (true) : (new_idx = (new_idx + 1) & idx_mask) { inline for (0..bucket_sectors) |new_s_idx| { const new_sec = sector(new_bs, new_idx, new_s_idx); if (new_sec.hash == 0) return sec.copy(new_sec); } } }