Skip to content

Lightpanda: the headless browser designed for AI and automation

perf: Improve Node.getDocument / ownerFrame performance

Karl Seguin2f955f4c9424
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 483e408f5..a3556a2c4 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -24,6 +24,7 @@ const Mime = @import("Mime.zig");
const Page = @import("Page.zig");
const Factory = @import("Factory.zig");
const Session = @import("Session.zig");
+const DocumentRegistry = @import("DocumentRegistry.zig");
const EventManager = @import("EventManager.zig");
const ScriptManager = @import("ScriptManager.zig");
const StyleManager = @import("StyleManager.zig");
@@ -105,6 +106,10 @@ page: *Page,
_session: *Session,
+// Node.getDocument is called a lot. Reference this here, from the Browser, to
+// avoid the _session.browser.documents jumps.
+documents: *DocumentRegistry,
+
_event_manager: EventManager,
_parse_mode: enum { document, fragment, document_write } = .document,
@@ -377,6 +382,7 @@ pub fn init(self: *Frame, frame_id: u32, page: *Page, opts: InitOpts) !void {
.local_arena = local_arena.allocator(),
._frame_id = frame_id,
._session = session,
+ .documents = &session.browser.documents,
._loader_id = loader_id,
._history_document_id = loader_id,
._factory = factory,
@@ -2797,6 +2803,7 @@ pub fn adoptNodeTree(self: *Frame, node: *Node, old_owner: *Document, new_owner:
// Per spec, adopted steps run on each element after its document is set.
if (node.is(Element)) |el| {
+ el._attributes.adopt(node._owner, self);
Element.Html.Custom.enqueueAdoptedCallbackOnElement(el, old_owner, new_owner, self);
// The shadow tree follows its host across documents: re-own it and
diff --git a/src/browser/tests/node/owner.html b/src/browser/tests/node/owner.html
index aafef51f8..f1ceadb57 100644
--- a/src/browser/tests/node/owner.html
+++ b/src/browser/tests/node/owner.html
@@ -69,6 +69,34 @@
}
</script>
+<script id=attr_follows_adoption>
+{
+ const doc = new DOMParser().parseFromString('<div a=1 b=2></div>', 'text/html');
+ const div = doc.querySelector('div');
+ // Handed out before the element moves.
+ const a = div.getAttributeNode('a');
+ testing.expectEqual(doc, a.ownerDocument);
+
+ document.adoptNode(div);
+ testing.expectEqual(document, a.ownerDocument);
+ testing.expectEqual(document, div.getAttributeNode('b').ownerDocument);
+
+ // Through insertion too, and on descendants.
+ const other = new DOMParser().parseFromString('<p c=3><i d=4></i></p>', 'text/html');
+ const p = other.querySelector('p');
+ const d = other.querySelector('i').getAttributeNode('d');
+ testing.expectEqual(other, d.ownerDocument);
+ document.body.appendChild(p);
+ testing.expectEqual(document, d.ownerDocument);
+
+ // A removed Attr keeps its node document.
+ p.firstChild.removeAttributeNode(d);
+ other.adoptNode(p);
+ testing.expectEqual(document, d.ownerDocument);
+ testing.expectEqual(other, p.getAttributeNode('c').ownerDocument);
+}
+</script>
+
<script id=iframe_document type=module>
{
const state = await testing.async();
diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zig
index 1c6a54689..b57c63640 100644
--- a/src/browser/webapi/Node.zig
+++ b/src/browser/webapi/Node.zig
@@ -719,27 +719,13 @@ pub fn ownerDocument(self: *const Node, frame: *const Frame) ?*Document {
if (self._type == .document) {
return null;
}
-
- // An attribute node follows its element (including across adoption).
- if (self._type == .attribute) {
- if (self.subtype(Element.Attribute)._element) |element| {
- return element.asNode().ownerDocument(frame);
- }
- }
-
- // The table is the browser's, so any frame of it will do.
- return frame._session.browser.documents.get(self._owner);
+ return self.getDocument(frame);
}
// The spec's "node document": every node has one, a document's is itself.
// `frame` only reaches the registry; any frame will do.
pub fn getDocument(self: *const Node, frame: *const Frame) *Document {
- if (self._type == .document) {
- return self.subtype(Document);
- }
- const doc = self.ownerDocument(frame);
- lp.assert(doc != null, "null node document", .{ .type = self.getNodeType() });
- return doc.?;
+ return frame.documents.get(self._owner);
}
// Returns the Frame that owns this node's tree, or null when the node's
diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zig
index 09cb04e02..01a3691e5 100644
--- a/src/browser/webapi/element/Attribute.zig
+++ b/src/browser/webapi/element/Attribute.zig
@@ -310,11 +310,25 @@ pub const List = struct {
const name = try self.put(attribute._name, attribute._value, element, frame);
attribute._element = element;
+ attribute.asNode()._owner = element.asNode()._owner;
const page = frame.page;
try page.attribute_lookup.put(page.arena, .{ .list = self, .name = name.ptr }, attribute);
return existing_attribute;
}
+ // An Attr's node document follows its element's: re-own the ones handed out
+ pub fn adopt(self: *const List, owner: u32, frame: *const Frame) void {
+ const lookup = &frame.page.attribute_lookup;
+ if (lookup.count() == 0) {
+ return;
+ }
+ for (self.entries()) |*entry| {
+ if (lookup.get(.{ .list = self, .name = entry._name_ptr })) |attr| {
+ attr.asNode()._owner = owner;
+ }
+ }
+ }
+
// called form our parser, names already lower-cased
pub fn putNew(self: *List, name: []const u8, value: []const u8, frame: *Frame) !void {
if (try self.getEntry(.wrap(name), frame) != null) {