summaryrefslogtreecommitdiff
path: root/src/zisp/gc/IstrSet.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/gc/IstrSet.zig')
-rw-r--r--src/zisp/gc/IstrSet.zig39
1 files changed, 23 insertions, 16 deletions
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;