summaryrefslogtreecommitdiff
path: root/src/zisp/gc/IstrSet.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-07-01 01:26:15 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-07-01 01:26:15 +0200
commit23b1ae89d351aabbdc8b7d6dfa362cc31165501e (patch)
tree5ab8e43a2ef7891d203e5925aedd0ddf44489884 /src/zisp/gc/IstrSet.zig
parentd5259ed20eae38bb4f732d829b927dd54cfac5f5 (diff)
Big changes. Incomplete. Won't compile yet.
Diffstat (limited to 'src/zisp/gc/IstrSet.zig')
-rw-r--r--src/zisp/gc/IstrSet.zig157
1 files changed, 0 insertions, 157 deletions
diff --git a/src/zisp/gc/IstrSet.zig b/src/zisp/gc/IstrSet.zig
deleted file mode 100644
index 179476e..0000000
--- a/src/zisp/gc/IstrSet.zig
+++ /dev/null
@@ -1,157 +0,0 @@
-//! Interned string set
-
-// TODO: Allocate the strings in tight blocks just like ListPool.
-
-const std = @import("std");
-
-const Alloc = std.mem.Allocator;
-
-const value = @import("../value.zig");
-
-const IstrPtr = value.istr.IstrPtr;
-const IstrHead = value.istr.IstrHead;
-
-const Set = @This();
-
-const max_fill_percent = 80;
-
-const Bucket = packed struct(u64) {
- ptr: u48 = 0,
- fp: u16 = 0,
-
- pub fn empty(self: Bucket) bool {
- return self.ptr == 0;
- }
-
- pub fn istrPtr(self: *const Bucket) IstrPtr {
- return @ptrFromInt(self.ptr);
- }
-
- pub fn putIstrPtr(self: *Bucket, istr: IstrPtr) void {
- self.ptr = @intCast(@intFromPtr(istr));
- }
-};
-
-alloc: Alloc,
-buckets: []Bucket = undefined,
-
-used_buckets: usize = 0,
-used_threshold: usize = undefined,
-
-const test_stdlib_impl = false;
-
-const default_bcount = 512;
-
-pub fn init(alloc: Alloc) !Set {
- return initCustom(alloc, default_bcount);
-}
-
-pub fn initCustom(alloc: Alloc, bcount: usize) !Set {
- if (test_stdlib_impl) {
- try map.ensureTotalCapacity(alloc, 512);
- return Set{ .alloc = alloc };
- }
- std.debug.assert(@popCount(bcount) == 1); // Must be power of 2.
- var self = Set{ .alloc = alloc };
- try self.allocBuckets(bcount);
- return self;
-}
-
-fn allocBuckets(self: *Set, bcount: usize) !void {
- self.buckets = try self.alloc.alloc(Bucket, bcount);
- @memset(self.buckets, Bucket{});
- self.used_threshold = bcount * max_fill_percent / 100;
-}
-
-pub fn deinit(self: *Set) void {
- self.alloc.free(self.buckets);
-}
-
-/// Get the istr with the given string contents, or alloc and store a new one.
-pub fn getOrNew(self: *Set, s: []const u8) !IstrPtr {
- std.debug.assert(s.len <= value.istr.max_len);
- if (test_stdlib_impl) {
- return self.addStdlib(s);
- }
- return self.getOrPutOrNew(s, null);
-}
-
-/// Get the canonical istr with the same contents, or store this one as it.
-pub fn getOrPut(self: *Set, istr: IstrPtr) !IstrPtr {
- return self.getOrPutOrNew(istr.bytes(), istr);
-}
-
-fn getOrPutOrNew(self: *Set, s: []const u8, ptr: ?IstrPtr) !IstrPtr {
- if (self.used_buckets > self.used_threshold) {
- try self.resize();
- }
-
- const hash = strHash(s);
- const fp = hashFp(hash);
-
- const idx_mask = self.buckets.len - 1;
- const idx_start = hash & idx_mask;
-
- var idx = idx_start;
- while (true) : (idx = (idx + 1) & idx_mask) {
- const bucket = &self.buckets[idx];
- if (bucket.empty()) {
- self.used_buckets += 1;
- const istr = ptr orelse try value.istr.new(self.alloc, s);
- bucket.putIstrPtr(istr);
- bucket.fp = fp;
- return istr;
- }
- if (bucket.fp == fp) {
- const istr = bucket.istrPtr();
- if (strEq(s, istr.bytes())) return istr;
- }
- }
-}
-
-fn resize(self: *Set) !void {
- const old_buckets = self.buckets;
- try self.allocBuckets(old_buckets.len << 1);
- defer self.alloc.free(old_buckets);
- const new_buckets = self.buckets;
-
- const idx_mask = new_buckets.len - 1;
- for (old_buckets) |old| {
- if (old.empty()) continue;
- const str = old.istrPtr().bytes();
- var idx = strHash(str) & idx_mask;
- while (!new_buckets[idx].empty()) idx = (idx + 1) & idx_mask;
- new_buckets[idx] = old;
- }
-}
-
-fn strHash(s: []const u8) u64 {
- return std.hash_map.hashString(s);
-}
-
-fn strEq(s: []const u8, s2: []const u8) bool {
- return std.hash_map.eqlString(s, s2);
-}
-
-fn hashFp(hash: u64) u16 {
- return @intCast(hash >> 48);
-}
-
-// Using stdlib, to compare
-
-const str_ctx = std.hash_map.StringContext{};
-
-const Map = std.hash_map.StringHashMapUnmanaged(IstrPtr);
-var map: Map = .empty;
-
-pub fn addStdlib(self: *Set, s: []const u8) !IstrPtr {
- const gop = map.getOrPutAdapted(self.alloc, s, str_ctx) catch @panic("OOM");
- if (gop.found_existing) {
- return gop.value_ptr.*;
- }
-
- const istr = try self.newIstr(s);
- gop.key_ptr.* = istr.bytes();
- gop.value_ptr.* = istr;
- return istr;
-}