diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2026-06-05 23:56:01 +0200 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2026-06-05 23:56:01 +0200 |
| commit | 3a66abbf1d54c441828d48a29dbed9fdde68da5b (patch) | |
| tree | 36ef28908d6db605c6c8b67956074f0014a866bb /src/zisp/gc | |
| parent | 3749ae3cdadc86656faeb642d5f260240ff18c0c (diff) | |
Big code cleanup; make stuff more configurable.
Diffstat (limited to 'src/zisp/gc')
| -rw-r--r-- | src/zisp/gc/IstrSet.zig | 45 | ||||
| -rw-r--r-- | src/zisp/gc/PairPool.zig | 35 |
2 files changed, 63 insertions, 17 deletions
diff --git a/src/zisp/gc/IstrSet.zig b/src/zisp/gc/IstrSet.zig index a97c258..b4575f6 100644 --- a/src/zisp/gc/IstrSet.zig +++ b/src/zisp/gc/IstrSet.zig @@ -33,6 +33,7 @@ const Alloc = std.mem.Allocator; const value = @import("../value.zig"); const IstrPtr = value.istr.IstrPtr; +const IstrHead = value.istr.IstrHead; const Set = @This(); @@ -54,6 +55,7 @@ const Bucket = [bucket_sectors][sector_u64_cnt]u64; const SectorPtr = *align(8) SectorHead; +// This must remain compatible with IstrHead! const SectorHead = packed union { hash: u64, meta: packed struct(u64) { @@ -97,7 +99,7 @@ const SectorHead = packed union { istr.putStr(s); return istr; } else { - const istr = try value.istr.new(alloc, hash, s); + const istr = try newIstr(alloc, hash, s); self.bufU64()[1] = @intFromPtr(istr); return istr; } @@ -107,8 +109,19 @@ const SectorHead = packed union { const end = sector_u64_cnt; @memcpy(dest.bufU64()[0..end], self.bufU64()[0..end]); } + + fn newIstr(alloc: Alloc, hash: u64, s: []const u8) !IstrPtr { + const algn = std.mem.Alignment.of(IstrPtr); + const size = @sizeOf(IstrHead) + s.len; + const ptr = try alloc.alignedAlloc(u8, algn, size); + const istr: IstrPtr = @ptrCast(ptr); + istr.* = .{ .hash = hash }; + istr.putStr(s); + return istr; + } }; +alloc: Alloc, buckets: []Bucket = undefined, used_sectors: usize = 0, @@ -122,19 +135,19 @@ pub fn init(alloc: Alloc) !Set { pub fn initCustom(alloc: Alloc, bcount: usize) !Set { std.debug.assert(@popCount(bcount) == 1); // Must be power of 2. - var self = Set{}; - try self.allocBuckets(alloc, bcount); + var self = Set{ .alloc = alloc }; + try self.allocBuckets(bcount); return self; } -fn allocBuckets(self: *Set, alloc: Alloc, bcount: usize) !void { - self.buckets = try alloc.alloc(Bucket, bcount); +fn allocBuckets(self: *Set, bcount: usize) !void { + self.buckets = try self.alloc.alloc(Bucket, bcount); @memset(self.buckets, std.mem.zeroes(Bucket)); self.used_threshold = bcount * bucket_sectors * max_fill_percent / 100; } -pub fn deinit(self: *Set, alloc: Alloc) void { - alloc.free(self.buckets); +pub fn deinit(self: *Set) void { + self.alloc.free(self.buckets); } fn sector(bs: []Bucket, b_idx: usize, s_idx: usize) SectorPtr { @@ -142,7 +155,7 @@ fn sector(bs: []Bucket, b_idx: usize, s_idx: usize) SectorPtr { } /// Add, or get existing, string. -pub fn add(self: *Set, alloc: Alloc, s: []const u8) !IstrPtr { +pub fn add(self: *Set, s: []const u8) !IstrPtr { std.debug.assert(s.len < 256); const idx_mask = self.buckets.len - 1; @@ -162,28 +175,28 @@ pub fn add(self: *Set, alloc: Alloc, s: []const u8) !IstrPtr { }; if (idx -% idx_init >= max_probe_length) { - try self.resize(alloc); - return self.add(alloc, s); + try self.resize(); + return self.add(s); } self.used_sectors += 1; if (self.used_sectors >= self.used_threshold) { - try self.resize(alloc); - return self.add(alloc, s); + try self.resize(); + return self.add(s); } const sec = sector(self.buckets, idx, s_idx); - return sec.insert(alloc, hash, s); + return sec.insert(self.alloc, hash, s); } -fn resize(self: *Set, alloc: Alloc) !void { +fn resize(self: *Set) !void { const old_bc = self.buckets.len; const old_bs = self.buckets; const new_bc = old_bc << 1; - try self.allocBuckets(alloc, new_bc); - defer alloc.free(old_bs); + try self.allocBuckets(new_bc); + defer self.alloc.free(old_bs); const new_bs = self.buckets; diff --git a/src/zisp/gc/PairPool.zig b/src/zisp/gc/PairPool.zig index 70b786d..8a437ae 100644 --- a/src/zisp/gc/PairPool.zig +++ b/src/zisp/gc/PairPool.zig @@ -1 +1,34 @@ -// TODO +const std = @import("std"); + +const value = @import("../value.zig"); + +const Alloc = std.mem.Allocator; +const PairMemPool = std.heap.MemoryPool(value.pair.Pair); + +const Value = value.Value; +const PairPtr = value.pair.PairPtr; + +const PairPool = @This(); + +alloc: Alloc, +pair_pool: PairMemPool, + +const default_init_cap = 1024; + +pub fn init(alloc: Alloc) !PairPool { + return initCustom(alloc, default_init_cap); +} + +pub fn initCustom(alloc: Alloc, init_cap: usize) !PairPool { + return .{ + .alloc = alloc, + .pair_pool = try PairMemPool.initCapacity(alloc, init_cap), + }; +} + +pub fn cons(self: *PairPool, car: Value, cdr: Value) !PairPtr { + var p = try self.pair_pool.create(self.alloc); + p.car = car; + p.cdr = cdr; + return p; +} |
