Lightpanda: the headless browser designed for AI and automation
perf: cache root node during parse
Karl Seguin3e20ba786f2e
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 11d70286c..bddb7b43c 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -2671,10 +2671,9 @@ pub fn notifyNetworkAlmostIdle(self: *Frame) void {// called from the parser. Text-node merging is the parser's responsibility// (see Parser.appendTextChunk in src/browser/parser/Parser.zig); this is the// "insert this fully-formed node as a new last child of parent" entry point.-pub fn appendNew(self: *Frame, parent: *Node, child: *Node) !void {+pub fn appendNew(self: *Frame, parent: *Node, child: *Node, parent_root: ?*Node) !void {lp.assert(child._parent == null, "Frame.appendNew", .{});- // opts is meaningless when from_parser (the first param) is true.- try self._insertNodeRelative(true, parent, child, .append, .{});+ try self._insertNodeRelative(true, parent, child, .append, .{ .parser_root = parent_root });}// called from the parser when the node and all its children have been added@@ -2994,6 +2993,8 @@ const InsertNodeOpts = struct {// the ready work itself once every node is in place, so an earlier// script observes its later siblings already inserted.run_ready: bool = true,+ // The parent's root (parser only, avoids having to look it up)+ parser_root: ?*Node = null,};pub fn insertNodeRelative(self: *Frame, parent: *Node, child: *Node, relative: InsertNodeRelative, opts: InsertNodeOpts) !void {return self._insertNodeRelative(false, parent, child, relative, opts);@@ -3066,14 +3067,14 @@ fn _insertNodeRelative(self: *Frame, comptime from_parser: bool, parent: *Node,}if (child.is(Element)) |el| {- // Invoke connectedCallback for custom elements during parsing.- // For main document parsing we know nodes are connected (fast path);- // for fragment parsing (innerHTML) we check connectivity.- if (child.isConnected() or child.isInShadowTree()) {+ const root = opts.parser_root orelse parent.getRootNode(.{});+ if (idMapsForRoot(root)) |id_maps| {if (el.getId()) |id| {- try self.addElementId(parent, el, id);+ try self.addElementIdWithMaps(id_maps, el, id);+ }+ if (rootIsConnected(root)) {+ try Element.Html.Custom.enqueueConnectedCallbackOnElement(true, el, self);}- try Element.Html.Custom.enqueueConnectedCallbackOnElement(true, el, self);}}return;diff --git a/src/browser/parser/Parser.zig b/src/browser/parser/Parser.zigindex e9778bb49..02e0929e5 100644--- a/src/browser/parser/Parser.zig+++ b/src/browser/parser/Parser.zig@@ -46,6 +46,10 @@ pub const ParsedNode = extern struct {// Set once html5ever has put the node in the tree or given it children.// Until then the node is unobservable and its document is only a guess.placed: bool = false,++ // Memo of the node's root, valid while page.dom_version == root_version.+ root: ?*Node = null,+ root_version: usize = 0,};// Attributes of an element the parser rebuilds (see settleDocument): the ones@@ -183,7 +187,7 @@ fn appendTextChunk(self: *Parser, parent: *Node, txt: []const u8) !void {// Fresh text run: the first chunk lives on _data only. buf stays empty// until (and unless) a second chunk arrives.const new_text = try Frame.node_factory.createTextNode(parent.getDocument(self.frame), txt);- try self.frame.appendNew(parent, new_text);+ try self.frame.appendNew(parent, new_text, null);self.inserted_since_checkpoint +|= 1;self.pending_text = .{.parent = parent,@@ -624,7 +628,7 @@ fn _appendDoctypeToDocument(self: *Parser, name: []const u8, public_id: []const});// Append it to the document- try frame.appendNew(self.container.node, doctype.asNode());+ try frame.appendNew(self.container.node, doctype.asNode(), null);}fn addAttrsIfMissingCallback(ctx: *anyopaque, target_ref: *anyopaque, attributes: h5e.AttributeIterator) callconv(.c) void {@@ -744,7 +748,10 @@ fn _appendCallback(self: *Parser, parent_pn: *ParsedNode, node_or_text: h5e.Nodereturn;}const child = try self.settleDocument(parent_pn, child_pn);- try self.frame.appendNew(parent, child);+ const root = self.rootOf(parent_pn);+ try self.frame.appendNew(parent, child, root);+ child_pn.root = root;+ child_pn.root_version = parent_pn.root_version;},.text => |txt| {const parent = parent_pn.node;@@ -755,6 +762,22 @@ fn _appendCallback(self: *Parser, parent_pn: *ParsedNode, node_or_text: h5e.Node}}+// Avoids walking to the root for every parsed element. When the parser inserts+// new node, the root node of existing nodes doesn't change and we don't bump+// dom_version, so we can cache this.+fn rootOf(self: *Parser, pn: *ParsedNode) *Node {+ const version = self.frame.page.dom_version;+ if (pn.root) |root| {+ if (pn.root_version == version) {+ return root;+ }+ }+ const root = pn.node.getRootNode(.{});+ pn.root = root;+ pn.root_version = version;+ return root;+}+fn settleDocument(self: *Parser, parent_pn: *ParsedNode, child_pn: *ParsedNode) !*Node {const frame = self.frame;const parent = parent_pn.node;diff --git a/src/browser/tests/custom_elements/connected_from_parser.html b/src/browser/tests/custom_elements/connected_from_parser.htmlindex 770c309b5..f2c575cfe 100644--- a/src/browser/tests/custom_elements/connected_from_parser.html+++ b/src/browser/tests/custom_elements/connected_from_parser.html@@ -119,4 +119,57 @@testing.expectEqual('value2', window.attrChangedCalls[1].newValue);}</script>++<script>+{+ window.disconnectedParseCalls = [];++ class DisconnectedParseElement extends HTMLElement {+ constructor() {+ super();+ // No id yet when constructed synchronously by the parser.+ window.disconnectedParseCalls.push('ctor');+ }+ connectedCallback() {+ window.disconnectedParseCalls.push('connected ' + this.id);+ }+ }++ customElements.define('disconnected-parse-element', DisconnectedParseElement);+}+</script>++<div id="detached-mid-parse"><script>document.getElementById('detached-mid-parse').remove();</script><disconnected-parse-element id="after-detach"></disconnected-parse-element></div>++<template id="parse-template"><disconnected-parse-element id="in-template"></disconnected-parse-element></template>++<script id="verify-disconnected-parse">+{+ // The parser kept appending into a parent a script had detached: the+ // element is constructed (synchronously, main-document parse) but never+ // connected.+ testing.expectEqual(null, document.getElementById('after-detach'));+ testing.expectEqual('ctor', window.disconnectedParseCalls.join(','));++ // Template contents are never connected (nor upgraded).+ testing.expectEqual(null, document.getElementById('in-template'));++ window.disconnectedParseCalls = [];++ // A shadow root scopes ids even when its host is detached, but nothing in+ // it is connected.+ const host = document.createElement('div');+ const shadow = host.attachShadow({mode: 'open'});+ shadow.innerHTML = '<disconnected-parse-element id="in-detached-shadow"></disconnected-parse-element>';+ testing.expectTrue(shadow.getElementById('in-detached-shadow') !== null);+ // (Fragment-parse constructors still run synchronously, so only check+ // that it isn't connected.)+ testing.expectEqual(-1, window.disconnectedParseCalls.indexOf('connected in-detached-shadow'));++ // Once the host is connected, so is the element.+ document.body.appendChild(host);+ testing.expectEqual('connected in-detached-shadow', window.disconnectedParseCalls.at(-1));+ host.remove();+}+</script></body>diff --git a/src/rust/html5ever/sink.rs b/src/rust/html5ever/sink.rsindex 746be9de7..709398e05 100644--- a/src/rust/html5ever/sink.rs+++ b/src/rust/html5ever/sink.rs@@ -50,6 +50,8 @@ struct ParsedNode {_node: *const c_void,data: *const ElementData,_placed: bool,+ _root: *const c_void,+ _root_version: usize,}fn element_data<'a>(target: Ref) -> &'a ElementData {