diff options
Diffstat (limited to 'src/zisp/gc')
| -rw-r--r-- | src/zisp/gc/IstrSet.zig | 8 | ||||
| -rw-r--r-- | src/zisp/gc/ListPool.zig | 69 | ||||
| -rw-r--r-- | src/zisp/gc/PairPool.zig | 34 |
3 files changed, 73 insertions, 38 deletions
diff --git a/src/zisp/gc/IstrSet.zig b/src/zisp/gc/IstrSet.zig index bc345f2..bf0db47 100644 --- a/src/zisp/gc/IstrSet.zig +++ b/src/zisp/gc/IstrSet.zig @@ -74,7 +74,7 @@ pub fn getOrNew(self: *Set, s: []const u8) !IstrPtr { /// 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.str(), istr); + return self.getOrPutOrNew(istr.bytes(), istr); } fn getOrPutOrNew(self: *Set, s: []const u8, ptr: ?IstrPtr) !IstrPtr { @@ -100,7 +100,7 @@ fn getOrPutOrNew(self: *Set, s: []const u8, ptr: ?IstrPtr) !IstrPtr { } if (bucket.fp == fp) { const istr = bucket.istrPtr(); - if (strEq(s, istr.str())) return istr; + if (strEq(s, istr.bytes())) return istr; } } } @@ -114,7 +114,7 @@ fn resize(self: *Set) !void { const idx_mask = new_buckets.len - 1; for (old_buckets) |old| { if (old.empty()) continue; - const str = old.istrPtr().str(); + 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; @@ -147,7 +147,7 @@ pub fn addStdlib(self: *Set, s: []const u8) !IstrPtr { } const istr = try self.newIstr(s); - gop.key_ptr.* = istr.str(); + gop.key_ptr.* = istr.bytes(); gop.value_ptr.* = istr; return istr; } diff --git a/src/zisp/gc/ListPool.zig b/src/zisp/gc/ListPool.zig new file mode 100644 index 0000000..6c34074 --- /dev/null +++ b/src/zisp/gc/ListPool.zig @@ -0,0 +1,69 @@ +//! List array pool +//! +//! Strategy: To ensure list arrays are allocated sequentially without any +//! padding, we allocate memory in large blocks, and whenever a list array with +//! a certain length is to be allocated, we check if the current block still has +//! enough room; otherwise we allocate the next block. +//! +//! This is only used for lists up to 7 elements. Starting at 8, they can't +//! share a cache line with other list elements anyway. + +const std = @import("std"); + +const Alloc = std.mem.Allocator; +const ArrayList = std.ArrayListUnmanaged; + +const value = @import("../value.zig"); + +const Value = value.Value; + +const Self = @This(); + +// How many Value elements fit in a block. A good value should be 512, since +// each Value is 8 bytes: 512 * 8 = 4 KiB +const block_value_cap = 512; + +// Initial capacity of ArrayList of pointers to filled blocks. Also 512, since +// pointers are 8 bytes as well. +const def_full_blocks_cap = 512; + +const Block = [block_value_cap]Value; + +alloc: Alloc, + +current_block: *Block, +current_index: usize, + +full_blocks: ArrayList(*Block), + +pub fn init(alloc: Alloc) !Self { + return .{ + .alloc = alloc, + .current_block = try alloc.create(Block), + .current_index = 0, + .full_blocks = try .initCapacity(alloc, def_full_blocks_cap), + }; +} + +pub fn deinit(self: *Self) void { + self.alloc.destroy(self.current_block); + for (self.full_blocks) |block| self.alloc.destroy(block); + self.full_blocks.deinit(self.alloc); +} + +fn newBlock(self: *Self) !void { + try self.full_blocks.append(self.alloc, self.current_block); + self.current_block = try self.alloc.create(Block); + self.current_index = 0; +} + +pub fn create(self: *Self, len: u3) ![*]Value { + std.debug.assert(len != 0); + + if (len > block_value_cap - self.current_index) { + try self.newBlock(); + } + + defer self.current_index += len; + return @ptrCast(&self.current_block[self.current_index]); +} diff --git a/src/zisp/gc/PairPool.zig b/src/zisp/gc/PairPool.zig deleted file mode 100644 index 4a77acc..0000000 --- a/src/zisp/gc/PairPool.zig +++ /dev/null @@ -1,34 +0,0 @@ -const std = @import("std"); - -const Alloc = std.mem.Allocator; -const AlignedPool = std.heap.memory_pool.Aligned; - -const value = @import("../value.zig"); - -const Value = value.Value; -const PairPtr = value.pair.PairPtr; - -const PairPool = @This(); - -alloc: Alloc, -pool: AlignedPool(value.pair.Pair, @enumFromInt(@alignOf(value.Zptr))), - -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, - .pool = try .initCapacity(alloc, init_cap), - }; -} - -pub fn cons(self: *PairPool, car: Value, cdr: Value) !PairPtr { - var p = try self.pool.create(self.alloc); - p.car = car; - p.cdr = cdr; - return p; -} |
