From 5e461592bfda4b42e6c11beaae1fa4b04f0aec95 Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Sun, 31 May 2026 14:35:49 +0200 Subject: [WORKING STATE] Fixes and cleanup. --- src/zisp/gc/IstrSet.zig | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'src/zisp/gc') diff --git a/src/zisp/gc/IstrSet.zig b/src/zisp/gc/IstrSet.zig index 465b0e3..0176291 100644 --- a/src/zisp/gc/IstrSet.zig +++ b/src/zisp/gc/IstrSet.zig @@ -13,7 +13,7 @@ //! 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 +//! 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 @@ -28,11 +28,11 @@ const std = @import("std"); -const value = @import("../value.zig"); - const Alloc = std.mem.Allocator; -const Istr = value.istr.Istr; +const value = @import("../value.zig"); + +const IstrPtr = value.istr.IstrPtr; const Set = @This(); @@ -51,7 +51,7 @@ const Sector = *align(8) packed union { return @ptrCast(self); } - fn getIstr(self: *@This()) Istr { + fn getIstrPtr(self: *@This()) IstrPtr { if (self.meta.len <= 24) { return @ptrCast(self); } else { @@ -59,11 +59,11 @@ const Sector = *align(8) packed union { } } - pub fn match(self: *@This(), hash: u64, s: []const u8) ?Istr { + pub fn match(self: *@This(), hash: u64, s: []const u8) ?IstrPtr { if (self.hash != hash) { return null; } - const istr = self.getIstr(); + const istr = self.getIstrPtr(); if (std.hash_map.eqlString(s, istr.str())) { return istr; } else { @@ -76,10 +76,10 @@ const Sector = *align(8) packed union { alloc: Alloc, hash: u64, s: []const u8, - ) !Istr { + ) !IstrPtr { self.hash = hash; if (s.len <= 24) { - const istr: Istr = @ptrCast(self); + const istr: IstrPtr = @ptrCast(self); istr.putStr(s); return istr; } else { @@ -90,7 +90,7 @@ const Sector = *align(8) packed union { } }; -buckets: []Bucket, +buckets: []Bucket = &.{}, const default_bcount = 512; @@ -99,26 +99,33 @@ pub fn init(alloc: Alloc) !Set { } pub fn initCustom(alloc: Alloc, bcount: usize) !Set { - return Set{ - .buckets = try alloc.alloc(Bucket, bcount), - }; + 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); } -pub fn sector(set: *Set, b_idx: usize, s_idx: usize) Sector { +fn sector(set: *Set, b_idx: usize, s_idx: usize) Sector { return @ptrCast(&set.buckets[b_idx][s_idx]); } -pub fn add(self: *Set, alloc: Alloc, s: []const u8) !Istr { +/// 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 << 1) | s.len; + 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; -- cgit v1.2.3