summaryrefslogtreecommitdiff
path: root/src/zisp/gc
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-22 18:22:36 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-22 18:22:36 +0200
commit476632d9559b1e3bacbd226df336116cac8544ea (patch)
tree5ad54d955ca676033538c9e93828093f039239d1 /src/zisp/gc
parent0245f1c238b232834a1a1b76d660aa3b626d832f (diff)
Improvements.
Diffstat (limited to 'src/zisp/gc')
-rw-r--r--src/zisp/gc/ListPool.zig25
1 files changed, 18 insertions, 7 deletions
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);
}