Skip to content

Lightpanda: the headless browser designed for AI and automation

design: rework cdata _type field

Karl Seguin678971e32004
diff --git a/src/browser/Factory.zig b/src/browser/Factory.zig
index 8066be305..5abf4c3ef 100644
--- a/src/browser/Factory.zig
+++ b/src/browser/Factory.zig
@@ -318,6 +318,58 @@ pub fn node(self: *Factory, child: anytype) !*@TypeOf(child) {
).create(allocator, child);
}
+// CData nodes: {EventTarget, Node, CData, [Text,] Leaf}.
+// CData is special, it's _type is a bare tag, not a tagged union. A website can
+// have tens of thousands of Text nodes, and this allows a few optimization to
+// both reduce the # of allocations and the size
+pub fn cdataNode(self: *Factory, cd: Node.CData, leaf: anytype) !*Node.CData {
+ const types = comptime prototypeTypes(@TypeOf(leaf));
+ comptime assert(types[0] == EventTarget and types[1] == Node and types[2] == Node.CData);
+
+ const chain = try PrototypeChain(types).allocate(self._slab.allocator());
+ chain.setRoot(EventTarget.Type);
+ chain.setMiddle(1, Node.Type);
+
+ const cd_ptr = chain.get(2);
+ cd_ptr.* = cd;
+ cd_ptr._proto = chain.get(1);
+
+ // only the CDATASection chain has a middle here (its Text)
+ inline for (3..types.len - 1) |i| {
+ chain.set(i, .{ ._proto = chain.get(i - 1) });
+ }
+ chain.setLeaf(types.len - 1, leaf);
+ return cd_ptr;
+}
+
+// The full type list for a leaf. Walks the Proto chain.
+// For example CData.Text -> [_]type{EventTarget, Node, CData, Text}).
+pub fn prototypeTypes(comptime Leaf: type) []const type {
+ comptime {
+ var types: []const type = &.{Leaf};
+ var T = Leaf;
+ while (reflect.Proto(T)) |P| {
+ T = P;
+ types = &[_]type{P} ++ types;
+ }
+ return types;
+ }
+}
+
+// Byte offset of T within the chain allocation for `Leaf`. This is what lets
+// a child reach its parent without a stored pointer.
+pub fn chainOffsetOf(comptime Leaf: type, comptime T: type) usize {
+ comptime {
+ var offset: usize = 0;
+ for (prototypeTypes(Leaf)) |C| {
+ offset = std.mem.alignForward(usize, offset, @alignOf(C));
+ if (C == T) return offset;
+ offset += @sizeOf(C);
+ }
+ @compileError(@typeName(T) ++ " is not in the prototype chain of " ++ @typeName(Leaf));
+ }
+}
+
pub fn document(self: *Factory, child: anytype) !*@TypeOf(child) {
const allocator = self._slab.allocator();
return try AutoPrototypeChain(
@@ -375,20 +427,11 @@ pub fn svgElement(self: *Factory, tag_name: []const u8, child: anytype) !*@TypeO
}
fn svgPrototypeTypes(comptime Child: type) []const type {
- comptime {
- var types: []const type = &[_]type{Child};
- var T = Child;
-
- while (T != EventTarget) {
- T = reflect.Proto(T) orelse @compileError(@typeName(T) ++ " does not lead to EventTarget through Proto");
- types = &[_]type{T} ++ types;
- }
-
- if (types.len < 5 or types[3] != Element.Svg) {
- @compileError(@typeName(Child) ++ " is not an SVG prototype chain");
- }
- return types;
+ const types = prototypeTypes(Child);
+ if (types.len < 5 or types[0] != EventTarget or types[3] != Element.Svg) {
+ @compileError(@typeName(Child) ++ " is not an SVG prototype chain");
}
+ return types;
}
pub fn xhrEventTarget(_: *const Factory, allocator: Allocator, child: anytype) !*@TypeOf(child) {
diff --git a/src/browser/frame/node_factory.zig b/src/browser/frame/node_factory.zig
index 3fdc09f45..361812b45 100644
--- a/src/browser/frame/node_factory.zig
+++ b/src/browser/frame/node_factory.zig
@@ -1051,26 +1051,20 @@ pub fn constructCustomElement(frame: *Frame, new_target: JS.Function) !*Element
}
pub fn createTextNode(frame: *Frame, text: []const u8) !*Node {
- const cd = try frame._factory.node(CData{
+ const cd = try frame._factory.cdataNode(.{
._proto = undefined,
- ._type = .{ .text = .{
- ._proto = undefined,
- } },
+ ._type = .text,
._data = try frame.dupeSSO(text),
- });
- cd._type.text._proto = cd;
+ }, CData.Text{ ._proto = undefined });
return cd.asNode();
}
pub fn createComment(frame: *Frame, text: []const u8) !*Node {
- const cd = try frame._factory.node(CData{
+ const cd = try frame._factory.cdataNode(.{
._proto = undefined,
- ._type = .{ .comment = .{
- ._proto = undefined,
- } },
+ ._type = .comment,
._data = try frame.dupeSSO(text),
- });
- cd._type.comment._proto = cd;
+ }, CData.Comment{ ._proto = undefined });
return cd.asNode();
}
@@ -1080,23 +1074,11 @@ pub fn createCDATASection(frame: *Frame, data: []const u8) !*Node {
return error.InvalidCharacterError;
}
- // First allocate the Text node separately
- const text_node = try frame._factory.create(CData.Text{
- ._proto = undefined,
- });
-
- // Then create the CData with cdata_section variant
- const cd = try frame._factory.node(CData{
+ const cd = try frame._factory.cdataNode(.{
._proto = undefined,
- ._type = .{ .cdata_section = .{
- ._proto = text_node,
- } },
+ ._type = .cdata_section,
._data = try frame.dupeSSO(data),
- });
-
- // Set up the back pointer from Text to CData
- text_node._proto = cd;
-
+ }, CData.CDATASection{ ._proto = undefined });
return cd.asNode();
}
@@ -1114,20 +1096,14 @@ pub fn createProcessingInstruction(frame: *Frame, target: []const u8, data: []co
const owned_target = try frame.dupeString(target);
- const pi = try frame._factory.create(CData.ProcessingInstruction{
+ const cd = try frame._factory.cdataNode(.{
._proto = undefined,
- ._target = owned_target,
- });
-
- const cd = try frame._factory.node(CData{
- ._proto = undefined,
- ._type = .{ .processing_instruction = pi },
+ ._type = .processing_instruction,
._data = try frame.dupeSSO(data),
+ }, CData.ProcessingInstruction{
+ ._proto = undefined,
+ ._target = owned_target,
});
-
- // Set up the back pointer from ProcessingInstruction to CData
- pi._proto = cd;
-
return cd.asNode();
}
diff --git a/src/browser/js/Local.zig b/src/browser/js/Local.zig
index 16710f82c..f3216a560 100644
--- a/src/browser/js/Local.zig
+++ b/src/browser/js/Local.zig
@@ -1259,7 +1259,19 @@ const Resolved = struct {
};
pub fn resolveValue(value: anytype) Resolved {
const T = bridge.Struct(@TypeOf(value));
- if (!@hasField(T, "_type") or @typeInfo(@TypeOf(value._type)) != .@"union") {
+ if (!@hasField(T, "_type")) {
+ return resolveT(T, value);
+ }
+
+ // A bare-tag _type means the subtypes are chain members, not payloads
+ // (e.g. CData); the type maps the tag to the member's type.
+ if (comptime @typeInfo(@TypeOf(value._type)) == .@"enum" and @hasDecl(T, "Subtype")) {
+ switch (value._type) {
+ inline else => |tag| return resolveValue(value.subtype(T.Subtype(tag))),
+ }
+ }
+
+ if (comptime @typeInfo(@TypeOf(value._type)) != .@"union") {
return resolveT(T, value);
}
diff --git a/src/browser/webapi/CData.zig b/src/browser/webapi/CData.zig
index 9458ff4bf..9ce8879ed 100644
--- a/src/browser/webapi/CData.zig
+++ b/src/browser/webapi/CData.zig
@@ -21,6 +21,7 @@ const lp = @import("lightpanda");
const js = @import("../js/js.zig");
const Frame = @import("../Frame.zig");
+const Factory = @import("../Factory.zig");
const Node = @import("Node.zig");
pub const Text = @import("cdata/Text.zig");
@@ -29,6 +30,7 @@ pub const CDATASection = @import("cdata/CDATASection.zig");
pub const ProcessingInstruction = @import("cdata/ProcessingInstruction.zig");
const String = lp.String;
+const IS_DEBUG = @import("builtin").mode == .Debug;
const CData = @This();
@@ -38,15 +40,48 @@ _type: Type,
_proto: *Node,
_data: String = .empty,
-pub const Type = union(enum) {
- text: Text,
- comment: Comment,
- // This should be under Text, but that would require storing a _type union
- // in text, which would add 8 bytes to every text node.
- cdata_section: CDATASection,
- processing_instruction: *ProcessingInstruction,
+// The subtype's struct is not a payload here: it's the member laid out after
+// the CData in the factory chain (see Factory.cdataNode), reachable via
+// `subtype`.
+pub const Type = enum(u8) {
+ text,
+ comment,
+ cdata_section,
+ processing_instruction,
};
+pub fn Subtype(comptime tag: Type) type {
+ return switch (tag) {
+ .text => Text,
+ .comment => Comment,
+ .cdata_section => CDATASection,
+ .processing_instruction => ProcessingInstruction,
+ };
+}
+
+fn tagOf(comptime T: type) Type {
+ if (T == Text) return .text;
+ if (T == Comment) return .comment;
+ if (T == CDATASection) return .cdata_section;
+ if (T == ProcessingInstruction) return .processing_instruction;
+ @compileError(@typeName(T) ++ " is not a CData subtype");
+}
+
+pub fn subtype(self: *CData, comptime T: type) *T {
+ const offset = comptime Factory.chainOffsetOf(T, T) - Factory.chainOffsetOf(T, CData);
+ const sub: *T = @ptrFromInt(@intFromPtr(self) + offset);
+ if (comptime IS_DEBUG) {
+ // the arithmetic rides on factory-chain contiguity; the stored
+ // back-pointer doubles as its canary
+ if (comptime T == CDATASection) {
+ std.debug.assert(sub._proto._proto == self);
+ } else {
+ std.debug.assert(sub._proto == self);
+ }
+ }
+ return sub;
+}
+
/// Count UTF-16 code units in a UTF-8 string.
/// 4-byte UTF-8 sequences (codepoints >= U+10000) produce 2 UTF-16 code units (surrogate pair),
/// everything else produces 1.
@@ -196,17 +231,10 @@ pub fn asNode(self: *CData) *Node {
}
pub fn is(self: *CData, comptime T: type) ?*T {
- inline for (@typeInfo(Type).@"union".fields) |f| {
- if (@field(Type, f.name) == self._type) {
- if (f.type == T) {
- return &@field(self._type, f.name);
- }
- if (f.type == *T) {
- return @field(self._type, f.name);
- }
- }
+ if (self._type != comptime tagOf(T)) {
+ return null;
}
- return null;
+ return self.subtype(T);
}
pub fn getData(self: *const CData) String {
@@ -297,7 +325,7 @@ pub fn format(self: *const CData, writer: *std.Io.Writer) !void {
.text => writer.print("<text>{f}</text>", .{self._data}),
.comment => writer.print("<!-- {f} -->", .{self._data}),
.cdata_section => writer.print("<![CDATA[{f}]]>", .{self._data}),
- .processing_instruction => |pi| writer.print("<?{s} {f}?>", .{ pi._target, self._data }),
+ .processing_instruction => writer.print("<?{s} {f}?>", .{ @constCast(self).subtype(ProcessingInstruction)._target, self._data }),
};
}
@@ -306,13 +334,15 @@ pub fn getLength(self: *const CData) usize {
}
pub fn isEqualNode(self: *const CData, other: *const CData) bool {
- if (std.meta.activeTag(self._type) != std.meta.activeTag(other._type)) {
+ if (self._type != other._type) {
return false;
}
if (self._type == .processing_instruction) {
@branchHint(.unlikely);
- if (std.mem.eql(u8, self._type.processing_instruction._target, other._type.processing_instruction._target) == false) {
+ const self_target = @constCast(self).subtype(ProcessingInstruction)._target;
+ const other_target = @constCast(other).subtype(ProcessingInstruction)._target;
+ if (std.mem.eql(u8, self_target, other_target) == false) {
return false;
}
// if the _targets are equal, we still want to compare the data
@@ -520,7 +550,7 @@ test "WebApi: CData.render" {
buffer.clearRetainingCapacity();
const cdata = CData{
- ._type = .{ .text = undefined },
+ ._type = .text,
._proto = undefined,
._data = .wrap(test_case.value),
};
diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zig
index 98a397f09..d310c69af 100644
--- a/src/browser/webapi/Node.zig
+++ b/src/browser/webapi/Node.zig
@@ -479,7 +479,7 @@ pub fn getNodeName(self: *const Node, buf: []u8) []const u8 {
.text => "#text",
.cdata_section => "#cdata-section",
.comment => "#comment",
- .processing_instruction => |pi| pi._target,
+ .processing_instruction => cd.subtype(CData.ProcessingInstruction)._target,
},
.document => "#document",
.document_type => |dt| dt.getName(),
@@ -1176,7 +1176,7 @@ pub fn cloneNode(self: *Node, deep_: ?bool, frame: *Frame) CloneError!*Node {
.text => Frame.node_factory.createTextNode(frame, data),
.cdata_section => Frame.node_factory.createCDATASection(frame, data),
.comment => Frame.node_factory.createComment(frame, data),
- .processing_instruction => |pi| Frame.node_factory.createProcessingInstruction(frame, pi._target, data),
+ .processing_instruction => Frame.node_factory.createProcessingInstruction(frame, cd.subtype(CData.ProcessingInstruction)._target, data),
};
},
.element => |el| return el.clone(deep, frame),
diff --git a/src/cdp/AXNode.zig b/src/cdp/AXNode.zig
index b20bfeda3..270de9439 100644
--- a/src/cdp/AXNode.zig
+++ b/src/cdp/AXNode.zig
@@ -988,8 +988,8 @@ fn writeName(
else => null,
},
.cdata => |cd| switch (cd._type) {
- .text => |*text| {
- try writeString(text.ownData(), w);
+ .text => {
+ try writeString(cd._data.str(), w);
return .contents;
},
else => null,
@@ -1108,8 +1108,8 @@ fn writeAccessibleNameFallback(node: *DOMNode, writer: *std.Io.Writer, frame: *F
while (it.next()) |child| {
switch (child._type) {
.cdata => |cd| switch (cd._type) {
- .text => |*text| {
- const content = std.mem.trim(u8, text.ownData(), &std.ascii.whitespace);
+ .text => {
+ const content = std.mem.trim(u8, cd._data.str(), &std.ascii.whitespace);
if (content.len > 0) {
try writer.writeAll(content);
try writer.writeByte(' ');