diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-06-01 16:47:50 +0200 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-06-01 16:47:50 +0200 |
| commit | 42faeb473d6902720c7ad0f1c86d4fabe7c100b3 (patch) | |
| tree | a2b1c1522872965801326eb42f81bf7eb5f51465 | |
| parent | 605dd18cd665fa84b48cac387adda1d4954b546c (diff) | |
IstrSet: Less hard-coded values.
| -rw-r--r-- | src/zisp/gc/IstrSet.zig | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/zisp/gc/IstrSet.zig b/src/zisp/gc/IstrSet.zig index 35365d1..005c220 100644 --- a/src/zisp/gc/IstrSet.zig +++ b/src/zisp/gc/IstrSet.zig @@ -36,7 +36,12 @@ const IstrPtr = value.istr.IstrPtr; const Set = @This(); -const Bucket = [2][4]u64; +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; @@ -52,7 +57,7 @@ const SectorHead = packed union { } fn getIstrPtr(self: SectorPtr) IstrPtr { - if (self.meta.len <= 24) { + if (self.meta.len <= sector_size - @sizeOf(SectorHead)) { return @ptrCast(self); } else { return @ptrFromInt(self.bufU64()[1]); @@ -78,7 +83,7 @@ const SectorHead = packed union { s: []const u8, ) !IstrPtr { self.hash = hash; - if (s.len <= 24) { + if (s.len <= sector_size - @sizeOf(SectorHead)) { const istr: IstrPtr = @ptrCast(self); istr.putStr(s); return istr; @@ -90,7 +95,8 @@ const SectorHead = packed union { } pub fn copy(self: SectorPtr, dest: SectorPtr) void { - @memcpy(dest.bufU64()[0..4], self.bufU64()[0..4]); + const end = sector_u64_cnt; + @memcpy(dest.bufU64()[0..end], self.bufU64()[0..end]); } }; @@ -136,7 +142,7 @@ pub fn add(self: *Set, alloc: Alloc, s: []const u8) !IstrPtr { var idx = idx_init; const s_idx = b: while (true) : (idx = (idx + 1) & idx_mask) { - inline for (0..1) |si| { + 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; @@ -154,8 +160,8 @@ pub fn add(self: *Set, alloc: Alloc, s: []const u8) !IstrPtr { self.used_sectors += 1; - // Remember sector count = 2 * bucket count, so 16/10 is really 8/10. - if (self.used_sectors >= (self.buckets.len * 16) / 10) { + // Resize at 80% fill rate + if (self.used_sectors >= (self.buckets.len * bucket_sectors * 8) / 10) { try self.resize(alloc); } @@ -178,7 +184,7 @@ fn resize(self: *Set, alloc: Alloc) !void { const idx_mask = new_bc - 1; for (old_bs, 0..) |_, idx| { - inline for (0..1) |s_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); } @@ -191,7 +197,7 @@ fn resize(self: *Set, alloc: Alloc) !void { 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..1) |new_s_idx| { + 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); } |
