Skip to content

Lightpanda: the headless browser designed for AI and automation

perf: Add a single threaded ArenaAllocator

Karl Seguin786ee388e5e1
diff --git a/src/Arena.zig b/src/Arena.zig
index 006d01307..579259ca2 100644
--- a/src/Arena.zig
+++ b/src/Arena.zig
@@ -26,9 +26,9 @@ const std = @import("std");
const lp = @import("lightpanda");
const ArenaPool = @import("ArenaPool.zig");
+const ArenaAllocator = @import("ArenaAllocator.zig");
const Allocator = std.mem.Allocator;
-const ArenaAllocator = std.heap.ArenaAllocator;
const Arena = @This();
diff --git a/src/ArenaAllocator.zig b/src/ArenaAllocator.zig
new file mode 100644
index 000000000..b141b5e8a
--- /dev/null
+++ b/src/ArenaAllocator.zig
@@ -0,0 +1,418 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// A single-threaded std.heap.ArenaAllocator. Same API and the same reset
+// semantics, minus the atomics std needs to be thread-safe.
+//
+// Allocation bumps `cur` towards `end`, both of which point into the newest
+// node. A reset that has nothing to give back (a single node already within
+// the retain limit) is O(1), so resetting an arena that wasn't used is cheap.
+
+// This came from digging into the known slow WPT tests:
+// `dom/nodes/NodeList-static-length-getter-tampered-*`
+// The idea was to skip resetting the arena in Caller.deinit if nothing had
+// been allocated (because std's ArenaAllocator reset isn't as cheap as it
+// could be in that case). Investigating why this was led to the fact that
+// in Zig 0.16 ArenaAllocator became thread-safe. Benchmarking this, using
+// those WPT tests, showed that a singled-threaded allocator would give us a
+// ~10% improvement in those specific tests.
+// The design is similar to the 0.15 ArenaAllocator, but it's designed so that
+// reset on no-allocation is cheaper.
+
+const std = @import("std");
+
+const mem = std.mem;
+const Allocator = std.mem.Allocator;
+const Alignment = std.mem.Alignment;
+
+const ArenaAllocator = @This();
+
+child_allocator: Allocator,
+
+// Newest node first. Allocations only ever come from this one.
+first: ?*Node = null,
+
+// The free range of `first`'s buffer, as addresses. Both 0 when there's no
+// node, which makes every allocation take the slow path without a null check.
+cur: usize = 0,
+end: usize = 0,
+
+const Node = struct {
+ next: ?*Node,
+ // Bytes allocated from the child, including this header.
+ size: usize,
+
+ fn allocatedSlice(node: *Node) []u8 {
+ return @as([*]u8, @ptrCast(node))[0..node.size];
+ }
+
+ fn bufStart(node: *Node) usize {
+ return @intFromPtr(node) + @sizeOf(Node);
+ }
+
+ fn capacity(node: *const Node) usize {
+ return node.size - @sizeOf(Node);
+ }
+};
+
+pub fn init(child_allocator: Allocator) ArenaAllocator {
+ return .{ .child_allocator = child_allocator };
+}
+
+pub fn deinit(self: ArenaAllocator) void {
+ var it = self.first;
+ while (it) |node| {
+ it = node.next;
+ self.child_allocator.rawFree(node.allocatedSlice(), .of(Node), @returnAddress());
+ }
+}
+
+pub fn allocator(self: *ArenaAllocator) Allocator {
+ return .{
+ .ptr = self,
+ .vtable = &.{
+ .alloc = alloc,
+ .resize = resize,
+ .remap = remap,
+ .free = free,
+ },
+ };
+}
+
+// Memory held from the child, excluding node headers.
+pub fn queryCapacity(self: *const ArenaAllocator) usize {
+ var capacity: usize = 0;
+ var it = self.first;
+ while (it) |node| : (it = node.next) {
+ capacity += node.capacity();
+ }
+ return capacity;
+}
+
+pub const ResetMode = std.heap.ArenaAllocator.ResetMode;
+
+// Frees every allocation. Returns false only if `retain_*` couldn't preheat
+// the arena; it is still usable then, just colder.
+pub fn reset(self: *ArenaAllocator, mode: ResetMode) bool {
+ const limit: usize = switch (mode) {
+ .free_all => {
+ self.deinit();
+ self.first = null;
+ self.cur = 0;
+ self.end = 0;
+ return true;
+ },
+ .retain_capacity => std.math.maxInt(usize),
+ .retain_with_limit => |limit| limit,
+ };
+
+ const first = self.first orelse return true;
+ if (first.next == null and first.capacity() <= limit) {
+ // The common case once an arena has warmed up: nothing to free or
+ // resize, just rewind.
+ self.cur = first.bufStart();
+ return true;
+ }
+ return self.resetSlow(first, limit);
+}
+
+fn resetSlow(self: *ArenaAllocator, first: *Node, limit: usize) bool {
+ @branchHint(.cold);
+
+ const child = self.child_allocator;
+ const total = self.queryCapacity();
+
+ // Keep the newest (largest) node, give back the rest.
+ var it = first.next;
+ while (it) |node| {
+ it = node.next;
+ child.rawFree(node.allocatedSlice(), .of(Node), @returnAddress());
+ }
+ first.next = null;
+
+ const new_size = mem.alignBackward(usize, @sizeOf(Node) + @min(limit, total), @alignOf(Node));
+ if (new_size <= @sizeOf(Node)) {
+ child.rawFree(first.allocatedSlice(), .of(Node), @returnAddress());
+ self.first = null;
+ self.cur = 0;
+ self.end = 0;
+ return true;
+ }
+
+ var ok = true;
+ var node = first;
+ if (node.size != new_size) {
+ if (child.rawResize(node.allocatedSlice(), .of(Node), new_size, @returnAddress())) {
+ node.size = new_size;
+ } else if (child.rawAlloc(new_size, .of(Node), @returnAddress())) |ptr| {
+ child.rawFree(node.allocatedSlice(), .of(Node), @returnAddress());
+ node = @ptrCast(@alignCast(ptr));
+ node.* = .{ .next = null, .size = new_size };
+ } else {
+ // Keep what we have.
+ ok = false;
+ }
+ }
+ self.setFirst(node);
+ return ok;
+}
+
+fn setFirst(self: *ArenaAllocator, node: *Node) void {
+ self.first = node;
+ self.cur = node.bufStart();
+ self.end = @intFromPtr(node) + node.size;
+}
+
+fn alloc(ctx: *anyopaque, n: usize, alignment: Alignment, ret_addr: usize) ?[*]u8 {
+ const self: *ArenaAllocator = @ptrCast(@alignCast(ctx));
+ _ = ret_addr;
+
+ const addr = alignment.forward(self.cur);
+ const new_cur = addr +% n;
+ if (new_cur <= self.end and new_cur >= addr) {
+ self.cur = new_cur;
+ return @ptrFromInt(addr);
+ }
+ return self.allocSlow(n, alignment);
+}
+
+noinline fn allocSlow(self: *ArenaAllocator, n: usize, alignment: Alignment) ?[*]u8 {
+ const child = self.child_allocator;
+ const prev_capacity: usize = if (self.first) |node| node.capacity() else 0;
+
+ // Same growth curve as std: half again the previous node, plus the request.
+ const min_size = @sizeOf(Node) + alignment.toByteUnits() + n;
+ const big_enough = prev_capacity + min_size + 16;
+ const size = mem.alignForward(usize, big_enough + big_enough / 2, @alignOf(Node));
+
+ // Grow the current node in place by the full step rather than just
+ // enough for `n`. After a retain_with_limit reset, libc keeps the slack of
+ // the old, larger node: growing by `n` would succeed every time and send
+ // every allocation past the limit through here.
+ if (self.first) |node| {
+ if (child.rawResize(node.allocatedSlice(), .of(Node), size, @returnAddress())) {
+ node.size = size;
+ self.end = @intFromPtr(node) + size;
+ return self.bump(n, alignment);
+ }
+ }
+
+ const ptr = child.rawAlloc(size, .of(Node), @returnAddress()) orelse return null;
+ const node: *Node = @ptrCast(@alignCast(ptr));
+ node.* = .{ .next = self.first, .size = size };
+ self.setFirst(node);
+ return self.bump(n, alignment);
+}
+
+// Only after allocSlow has made room.
+fn bump(self: *ArenaAllocator, n: usize, alignment: Alignment) [*]u8 {
+ const addr = alignment.forward(self.cur);
+ std.debug.assert(addr + n <= self.end);
+ self.cur = addr + n;
+ return @ptrFromInt(addr);
+}
+
+fn resize(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) bool {
+ const self: *ArenaAllocator = @ptrCast(@alignCast(ctx));
+ _ = alignment;
+ _ = ret_addr;
+
+ const start = @intFromPtr(memory.ptr);
+ if (start + memory.len != self.cur) {
+ // Not the most recent allocation: can shrink (by doing nothing), can't grow.
+ return new_len <= memory.len;
+ }
+
+ if (new_len <= memory.len or new_len - memory.len <= self.end - self.cur) {
+ self.cur = start + new_len;
+ return true;
+ }
+ return false;
+}
+
+fn remap(ctx: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
+ return if (resize(ctx, memory, alignment, new_len, ret_addr)) memory.ptr else null;
+}
+
+fn free(ctx: *anyopaque, memory: []u8, alignment: Alignment, ret_addr: usize) void {
+ const self: *ArenaAllocator = @ptrCast(@alignCast(ctx));
+ _ = alignment;
+ _ = ret_addr;
+
+ // Only the most recent allocation can be given back.
+ if (@intFromPtr(memory.ptr) + memory.len == self.cur) {
+ self.cur = @intFromPtr(memory.ptr);
+ }
+}
+
+const testing = std.testing;
+
+test "ArenaAllocator: alloc, alignment and growth" {
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+
+ var rng = std.Random.DefaultPrng.init(testing.random_seed);
+ const random = rng.random();
+ for (0..2000) |_| {
+ const size = random.intRangeAtMost(usize, 1, 3000);
+ const slice = try a.alignedAlloc(u8, .@"32", size);
+ try testing.expect(Alignment.@"32".check(@intFromPtr(slice.ptr)));
+ @memset(slice, 0xAA);
+ }
+ try testing.expect(arena.first.?.next != null);
+}
+
+test "ArenaAllocator: reset with preheating" {
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+
+ var rng = std.Random.DefaultPrng.init(testing.random_seed);
+ const random = rng.random();
+ for (0..25) |_| {
+ _ = arena.reset(.retain_capacity);
+ var allocated: usize = 0;
+ const total = random.intRangeAtMost(usize, 256, 16384);
+ while (allocated < total) {
+ const size = random.intRangeAtMost(usize, 16, 256);
+ const slice = try arena.allocator().alignedAlloc(u8, .@"32", size);
+ try testing.expect(Alignment.@"32".check(@intFromPtr(slice.ptr)));
+ allocated += slice.len;
+ }
+ }
+ // After warming up, everything fits in one node.
+ _ = arena.reset(.retain_capacity);
+ try testing.expect(arena.first.?.next == null);
+}
+
+test "ArenaAllocator: reset while retaining a buffer" {
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+
+ _ = try a.alloc(u8, 1);
+ while (arena.first.?.next == null) {
+ _ = try a.alloc(u8, 1000);
+ }
+
+ try testing.expect(arena.reset(.{ .retain_with_limit = 16 }));
+ try testing.expect(arena.first.?.next == null);
+ try testing.expectEqual(16, arena.queryCapacity());
+}
+
+test "ArenaAllocator: reset of an unused arena" {
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+
+ try testing.expect(arena.reset(.{ .retain_with_limit = 1024 }));
+ try testing.expect(arena.first == null);
+
+ const one = try arena.allocator().alloc(u8, 100);
+ const node = arena.first.?;
+ try testing.expect(arena.reset(.{ .retain_with_limit = 1024 }));
+ try testing.expectEqual(node, arena.first.?);
+ try testing.expectEqual(node.bufStart(), arena.cur);
+
+ // Rewound: the next allocation reuses the same memory.
+ const two = try arena.allocator().alloc(u8, 100);
+ try testing.expectEqual(one.ptr, two.ptr);
+
+ try testing.expect(arena.reset(.free_all));
+ try testing.expect(arena.first == null);
+ try testing.expect(arena.reset(.free_all));
+}
+
+test "ArenaAllocator: limit shrinks a large single node" {
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+
+ _ = try arena.allocator().alloc(u8, 10_000);
+ try testing.expect(arena.queryCapacity() > 1024);
+ try testing.expect(arena.reset(.{ .retain_with_limit = 1024 }));
+ try testing.expectEqual(1024, arena.queryCapacity());
+
+ try testing.expect(arena.reset(.{ .retain_with_limit = 0 }));
+ try testing.expect(arena.first == null);
+}
+
+test "ArenaAllocator: growth past a reset limit doesn't take the slow path per allocation" {
+ // libc can't shrink in place, so after a retain_with_limit reset the kept
+ // node still has the old node's slack behind it.
+ var arena = ArenaAllocator.init(std.heap.c_allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+
+ _ = try a.alloc(u8, 64 * 1024);
+ try testing.expect(arena.reset(.{ .retain_with_limit = 16 * 1024 }));
+
+ var slow: usize = 0;
+ var end = arena.end;
+ for (0..1000) |_| {
+ _ = try a.alloc(u8, 48);
+ if (arena.end != end) {
+ slow += 1;
+ end = arena.end;
+ }
+ }
+ try testing.expect(slow < 5);
+}
+
+test "ArenaAllocator: resize and free of the last allocation" {
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+
+ _ = try a.alloc(u8, 1); // so the next allocation isn't at the buffer start
+ var buf = try a.alloc(u8, 10);
+ const before = arena.cur;
+
+ try testing.expect(a.resize(buf, 20));
+ buf = buf.ptr[0..20];
+ try testing.expectEqual(before + 10, arena.cur);
+
+ try testing.expect(a.resize(buf, 5));
+ buf = buf.ptr[0..5];
+ try testing.expectEqual(before - 5, arena.cur);
+
+ a.free(buf);
+ try testing.expectEqual(@intFromPtr(buf.ptr), arena.cur);
+
+ // Not the last allocation: shrink is a no-op success, grow fails.
+ const x = try a.alloc(u8, 8);
+ _ = try a.alloc(u8, 8);
+ try testing.expect(a.resize(x, 4));
+ try testing.expect(a.resize(x, 16) == false);
+
+ // Growing past the node's end fails.
+ const last = try a.alloc(u8, 8);
+ try testing.expect(a.resize(last, 1 << 30) == false);
+}
+
+test "ArenaAllocator: ArrayList growth" {
+ var arena = ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+
+ var list: std.ArrayList(u32) = .empty;
+ for (0..10_000) |i| {
+ try list.append(a, @intCast(i));
+ }
+ for (list.items, 0..) |v, i| {
+ try testing.expectEqual(@as(u32, @intCast(i)), v);
+ }
+}
diff --git a/src/ArenaPool.zig b/src/ArenaPool.zig
index 605abaee3..ecf644d22 100644
--- a/src/ArenaPool.zig
+++ b/src/ArenaPool.zig
@@ -20,10 +20,10 @@ const std = @import("std");
const lp = @import("lightpanda");
const Arena = @import("Arena.zig");
+const ArenaAllocator = @import("ArenaAllocator.zig");
const log = lp.log;
const Allocator = std.mem.Allocator;
-const ArenaAllocator = std.heap.ArenaAllocator;
const ArenaPool = @This();
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zig
index c0057ae68..b36fe57e1 100644
--- a/src/browser/js/Caller.zig
+++ b/src/browser/js/Caller.zig
@@ -18,7 +18,9 @@
const std = @import("std");
const lp = @import("lightpanda");
+
const string = @import("../../string.zig");
+const ArenaAllocator = @import("../../ArenaAllocator.zig");
const Page = @import("../Page.zig");
const Frame = @import("../Frame.zig");
@@ -32,7 +34,6 @@ const TaggedOpaque = @import("TaggedOpaque.zig");
const v8 = js.v8;
const log = lp.log;
-const ArenaAllocator = std.heap.ArenaAllocator;
const CALL_ARENA_RETAIN = 1024 * 16;
const LOCAL_ARENA_RETAIN = 1024 * 16;