summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/zisp/gc/IstrSet.zig28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/zisp/gc/IstrSet.zig b/src/zisp/gc/IstrSet.zig
index 4146713..4357608 100644
--- a/src/zisp/gc/IstrSet.zig
+++ b/src/zisp/gc/IstrSet.zig
@@ -34,6 +34,8 @@ 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 {
@@ -41,6 +43,10 @@ pub fn init(alloc: Alloc) !Set {
}
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);
@@ -59,6 +65,9 @@ pub fn deinit(self: *Set) void {
/// Get the istr with the given string contents, or alloc and store a new one.
pub fn getOrNew(self: *Set, s: []const u8) !IstrPtr {
+ if (test_stdlib_impl) {
+ return self.addStdlib(s);
+ }
std.debug.assert(s.len < 256);
return self.getOrNewOrPut(s, null);
}
@@ -131,3 +140,22 @@ fn strEq(s: []const u8, s2: []const u8) bool {
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.str();
+ gop.value_ptr.* = istr;
+ return istr;
+}