summaryrefslogtreecommitdiff
path: root/src/zisp/gc
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/gc')
-rw-r--r--src/zisp/gc/IstrSet.zig109
1 files changed, 59 insertions, 50 deletions
diff --git a/src/zisp/gc/IstrSet.zig b/src/zisp/gc/IstrSet.zig
index 31bc850..465b0e3 100644
--- a/src/zisp/gc/IstrSet.zig
+++ b/src/zisp/gc/IstrSet.zig
@@ -1,32 +1,39 @@
-///
-/// Efficient hash set for short(ish) strings.
-///
-/// Layout: []Bucket
-///
-/// * Bucket: [2]Sector
-///
-/// * Sector: [32]u8 or [4]u64
-///
-/// The parser interns strings up to 255 bytes in length, using the `Istr` heap
-/// representation, which has an 8-byte metadata prefix. Therefore, strings of
-/// up to 24 bytes can fit into a 32-byte sector as an immediate Istr, so the
-/// 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
-///
-/// 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
-/// empty is a matter of checking its first 8 bytes as a u64 value, which would
-/// hold the Istr header, which cannot be zero, because the lowest 8 bits are
-/// the length of the string.
-///
-/// If the length value is greater than 24, meaning the Istr can't actually fit
-/// here, that means the next 8 bytes of the sector are actually a pointer to
-/// the Istr elsewhere on the heap, and another 16 bytes are unused.
-///
+//!
+//! Efficient hash set for short(ish) strings.
+//!
+//! Layout: []Bucket
+//!
+//! * Bucket: [2]Sector
+//!
+//! * Sector: [32]u8 or [4]u64
+//!
+//! The parser interns strings up to 255 bytes in length, using the `Istr` heap
+//! representation, which has an 8-byte metadata prefix. Therefore, strings of
+//! up to 24 bytes can fit into a 32-byte sector as an immediate Istr, so the
+//! 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
+//!
+//! 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
+//! empty is a matter of checking its first 8 bytes as a u64 value, which would
+//! hold the Istr header, which cannot be zero, because the lowest 8 bits are
+//! the length of the string.
+//!
+//! If the length value is greater than 24, meaning the Istr can't actually fit
+//! here, that means the next 8 bytes of the sector are actually a pointer to
+//! the Istr elsewhere on the heap, and another 16 bytes are unused.
+//!
+
const std = @import("std");
+const value = @import("../value.zig");
+
+const Alloc = std.mem.Allocator;
+
+const Istr = value.istr.Istr;
+
const Set = @This();
const Bucket = [2][4]u64;
@@ -48,13 +55,13 @@ const Sector = *align(8) packed union {
if (self.meta.len <= 24) {
return @ptrCast(self);
} else {
- return @bitCast(self.bufU64()[1]);
+ return @ptrFromInt(self.bufU64()[1]);
}
}
pub fn match(self: *@This(), hash: u64, s: []const u8) ?Istr {
if (self.hash != hash) {
- return false;
+ return null;
}
const istr = self.getIstr();
if (std.hash_map.eqlString(s, istr.str())) {
@@ -66,18 +73,18 @@ const Sector = *align(8) packed union {
pub fn insert(
self: *@This(),
- alloc: *Allocator,
+ alloc: Alloc,
hash: u64,
s: []const u8,
- ) Istr {
+ ) !Istr {
self.hash = hash;
if (s.len <= 24) {
- const istr = @ptrCast(self);
+ const istr: Istr = @ptrCast(self);
istr.putStr(s);
return istr;
} else {
- const istr = Istr.new(alloc, hash, s);
- self.bufU64()[1] = @bitCast(istr);
+ const istr = try value.istr.new(alloc, hash, s);
+ self.bufU64()[1] = @intFromPtr(istr);
return istr;
}
}
@@ -87,53 +94,55 @@ buckets: []Bucket,
const default_bcount = 512;
-pub fn init(alloc: *Allocator) !Set {
+pub fn init(alloc: Alloc) !Set {
return initCustom(alloc, default_bcount);
}
-pub fn initCustom(alloc: *Allocator, bcount: usize) !Set {
+pub fn initCustom(alloc: Alloc, bcount: usize) !Set {
return Set{
.buckets = try alloc.alloc(Bucket, bcount),
};
}
-pub fn deinit(self: *Set, alloc: *Allocator) void {
+pub fn deinit(self: *Set, alloc: Alloc) void {
alloc.free(self.buckets);
}
-pub fn add(self: *Set, s: []const u8) Istr {
+pub 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 {
std.debug.assert(s.len < 256);
const idx_mask = self.buckets.len - 1;
- const len = s.len;
-
const h0 = std.hash_map.hashString(s);
- const hash = (h0 << 1) | len;
+ const hash = (h0 << 1) | s.len;
// We resize if we had to walk through 50% of the buckets.
- const probe_limit = self.buckets.len / 2;
+ const probe_limit = self.buckets.len >> 1;
var probes: usize = 0;
var idx = @as(usize, @truncate(hash & idx_mask));
- var s_idx: usize = undefined;
- while (true) : (idx = (idx + 1) & idx_mask) {
+ const s_idx = b: while (true) : (idx = (idx + 1) & idx_mask) {
inline for (0..1) |i| {
- const sec: Sector = @ptrCast(&self.buckets[idx][i]);
- if (sec.hash == 0) { s_idx = i; break; }
- if (sec.match(s)) |istr| return istr;
+ const sec = sector(self, idx, i);
+ if (sec.hash == 0) break :b i;
+ if (sec.match(hash, s)) |istr| return istr;
}
probes += 1;
if (probes > probe_limit) {
self.resize();
- return self.add(s);
+ return self.add(alloc, s);
}
};
- const sec = &self.buckets[idx][s_idx];
+ const sec = sector(self, idx, s_idx);
return sec.insert(alloc, hash, s);
}
-fn resize() void {
+fn resize(self: *Set) void {
+ _ = self;
@panic("IstrSet resize not implemented.");
}