From 476632d9559b1e3bacbd226df336116cac8544ea Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Mon, 22 Jun 2026 18:22:36 +0200 Subject: Improvements. --- src/zisp/gc/ListPool.zig | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'src/zisp/gc') diff --git a/src/zisp/gc/ListPool.zig b/src/zisp/gc/ListPool.zig index a843ae5..0735582 100644 --- a/src/zisp/gc/ListPool.zig +++ b/src/zisp/gc/ListPool.zig @@ -21,11 +21,8 @@ const SegStack = seg_stack.SegStack; 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; - -const Block = [block_value_cap]Value; +// 4 KiB blocks fit 512 Value elements, which should be good. +const Block = [4096 / @sizeOf(Value)]Value; alloc: Alloc, @@ -65,6 +62,10 @@ fn newBlock(self: *Self) !void { self.current_index = 0; } +fn curBlockPtr(self: *Self) [*]Value { + return @ptrCast(&self.current_block[self.current_index]); +} + fn popFree(self: *Self, len: u3) ?[*]Value { return self.free_lists[len - 1].pop(self.alloc); } @@ -78,17 +79,27 @@ pub fn allocVals(self: *Self, len: u3) ![*]Value { if (self.popFree(len)) |ptr| return ptr; - if (len > block_value_cap - self.current_index) { + if (len > self.current_block.len - self.current_index) { try self.newBlock(); } defer self.current_index += len; - return @ptrCast(&self.current_block[self.current_index]); + return self.curBlockPtr(); } pub fn freeVals(self: *Self, len: u3, ptr: [*]Value) !void { std.debug.assert(len != 0); + // Is it simply the last allocation? If so, just undo it. + if (self.current_index >= len) { + const ptr_val = @intFromPtr(ptr); + const last_ptr_val = @intFromPtr(self.curBlockPtr() - len); + if (ptr_val == last_ptr_val) { + self.current_index -= len; + return; + } + } + try self.pushFree(len, ptr); } -- cgit v1.2.3