From 3b1e6547d069d7c438af551a4989972802a895ee Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 28 Apr 2026 15:08:02 +0200 Subject: perf(html): reduce memory usage --- src/eval/html/Void.zig | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/eval/html/Void.zig') diff --git a/src/eval/html/Void.zig b/src/eval/html/Void.zig index ec35fc7..99bc923 100644 --- a/src/eval/html/Void.zig +++ b/src/eval/html/Void.zig @@ -3,12 +3,17 @@ const Allocator = std.mem.Allocator; const List = std.ArrayList; const html = @import("html.zig"); const Element = @import("Element.zig"); +const Node = Element.Node; const Error = Element.Error; alloc: Allocator, tag: []const u8, attributes: std.StringArrayHashMap([]const u8), class_list: std.BufSet, +node: Node = .{ + .ptr = undefined, + .vtable = .{ .element = fromNode }, +}, pub const Self = @This(); @@ -20,11 +25,12 @@ pub fn init(alloc: Allocator, tag: []const u8) Error!*Self { .attributes = .init(alloc), .class_list = .init(alloc), }; + v.node.ptr = v; return v; } pub fn element(self: *Self) Element { - return .{ .vtable = .{ .render = Self.render }, .ptr = self }; + return .{ .vtable = .{ .render = render, .node = getNode }, .ptr = self }; } pub fn setAttribute(self: *Self, k: []const u8, v: []const u8) Error!void { @@ -51,6 +57,16 @@ pub fn removeClass(self: *Self, v: []const u8) void { self.class_list.remove(v); } +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 render(context: *anyopaque, alloc: Allocator) Error![]const u8 { const self: *Self = @ptrCast(@alignCast(context)); const attr = try renderAttribute(alloc, &self.attributes, &self.class_list); -- cgit v1.2.3