diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-28 19:37:37 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-28 19:37:37 +0200 |
| commit | d9e37656e83d7d3c25709795ab7ccccca0071254 (patch) | |
| tree | 622789be5f608f39d6fdd9b9b99a16209f3b1d7f /src/eval/Root.zig | |
| parent | 3b1e6547d069d7c438af551a4989972802a895ee (diff) | |
perf(eval): reduce memory usage
Diffstat (limited to 'src/eval/Root.zig')
| -rw-r--r-- | src/eval/Root.zig | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/eval/Root.zig b/src/eval/Root.zig index fc59c1d..d4ccf30 100644 --- a/src/eval/Root.zig +++ b/src/eval/Root.zig @@ -3,21 +3,23 @@ const Allocator = std.mem.Allocator; const Arena = std.heap.ArenaAllocator; const HTML = @import("html/Element.zig"); const Element = @import("Element.zig"); +const Node = Element.Node; const Self = @This(); -content: std.ArrayList(Element), +content: std.DoublyLinkedList = .{}, arena: Arena, +node: Node = .{ + .ptr = undefined, + .vtable = .{ .element = fromNode }, +}, pub fn init(parent: Allocator) !*Self { - var s = Self{ - .content = undefined, - .arena = .init(parent), - }; + var s = Self{ .arena = .init(parent) }; var alloc = s.arena.allocator(); - s.content = try .initCapacity(alloc, 2); const v = try alloc.create(Self); v.* = s; + v.node.ptr = v; return v; } @@ -29,22 +31,32 @@ pub fn allocator(self: *Self) Allocator { return self.arena.allocator(); } -pub fn append(self: *Self, el: Element) !void { - try self.content.append(self.allocator(), el); +pub fn append(self: *Self, el: Element) void { + self.content.append(&el.node().node); } pub fn element(self: *Self) Element { - return .{ .vtable = .{ .html = html }, .ptr = self }; + return .{ .vtable = .{ .html = html, .node = getNode }, .ptr = self }; } pub fn renderHTML(self: *Self, alloc: Allocator) HTML.Error![]const u8 { return try self.element().renderHTML(alloc); } +fn getNode(context: *anyopaque) *Node { + const self: *Self = @ptrCast(@alignCast(context)); + return &self.node; +} + +fn fromNode(context: *anyopaque) Element { + const self: *Self = @ptrCast(@alignCast(context)); + return self.element(); +} + fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML { const self: *Self = @ptrCast(@alignCast(context)); const el = try HTML.Root.init(alloc); - if (self.content.items.len == 0) return el.element(); - for (self.content.items) |it| el.append(try it.html(el.allocator())); + var v = self.content.first; + while (v) |it| : (v = it.next) el.append(try Node.from(it).element().html(el.allocator())); return el.element(); } |
