Lightpanda: the headless browser designed for AI and automation
perf: cache computed ancestry values
Karl Seguin89ceb8f99aa3
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zigindex 853a08c7a..cc11f6a59 100644--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@ -622,19 +622,16 @@ fn rebuildIfDirty(self: *StyleManager) !void {pub fn isHidden(self: *StyleManager, el: *Element, options: CheckVisibilityOptions) bool {self.assertOwns(el);self.rebuildIfDirty() catch return false;- if (!options.ancestors) {- return self.visibilityProps(el).probe(.hidden, options);- }- return self.anyInChain(el, .hidden, options);+ return self.visibilityProps(el).isHidden(options);}-/// Computed display:none for a single element (own property, no ancestor walk).+/// The element's own display:none. Use `isHidden` to see if it's hidden by an ancestor/// Honors the UA stylesheet rules per HTML Rendering §15.3.1 "Hidden elements".pub fn hasDisplayNone(self: *StyleManager, el: *Element) bool {return self.display(el) == .none;}-/// Own property, no ancestor walk; honors the UA hidden-element rules.+/// Own property, not inherited; honors the UA hidden-element rules.pub fn display(self: *StyleManager, el: *Element) Display {self.assertOwns(el);self.rebuildIfDirty() catch return .other;@@ -652,20 +649,19 @@ pub fn hasAuthorDisplayNone(self: *StyleManager, el: *Element) bool {return p.author_display and p.display == .none;}-/// Computed visibility:hidden for an element, considering only the `visibility`-/// chain (walks ancestors since `visibility` inherits by default). Ignores+/// Computed visibility:hidden, which inherits from the ancestors. Ignores/// display:none: an ancestor with display:none means the element isn't/// rendered, but its computed `visibility` still reflects inherited visibility.pub fn hasVisibilityHiddenInherited(self: *StyleManager, el: *Element) bool {self.assertOwns(el);self.rebuildIfDirty() catch return false;- return self.anyInChain(el, .visibility, .{});+ return self.visibilityProps(el).visibility_hidden;}pub fn hasPointerEventsNone(self: *StyleManager, el: *Element) bool {self.assertOwns(el);self.rebuildIfDirty() catch return false;- return self.anyInChain(el, .pointer_events, .{});+ return self.visibilityProps(el).pointer_events_none;}/// The axes along which `el` is a scroll container: its own computed overflow@@ -701,19 +697,68 @@ pub fn inlineSize(self: *StyleManager, el: *Element, comptime axis: Element.Axisreturn length.resolve(self.frame.page.getViewport());}-fn anyInChain(self: *StyleManager, el: *Element, comptime what: Visibility.Probe, options: CheckVisibilityOptions) bool {+fn visibilityProps(self: *StyleManager, el: *Element) Visibility.Computed {+ const Inherited = std.meta.FieldEnum(Visibility.Computed);+ const inherited = Visibility.Computed.inherited;+ const group = &self.visibility;+ const allocator = self.arena.allocator();++ var base: Visibility.Computed = .{};+ var reach: std.EnumArray(Inherited, u32) = .initFill(0);+ var own: Visibility.Computed = .{};+ var len: u32 = 0;++ // Up to the first memoized ancestorvar current: ?*Element = el;while (current) |elem| : (current = elem.parentElement()) {- if (self.visibilityProps(elem).probe(what, options)) {- return true;+ const entry = group.memoEntry(allocator, elem, self.frame);+ if (entry) |gop| {+ if (gop.found_existing) {+ base = gop.value_ptr.*;+ break;+ }+ }+ const props: Visibility.Computed = .fromOwn(group.compute(elem, self.frame));+ if (entry) |gop| {+ gop.value_ptr.* = props;+ }+ if (len == 0) {+ own = props;+ }++ len += 1;+ inline for (inherited) |field| {+ if (@field(props, field)) {+ reach.set(@field(Inherited, field), len);+ }}}- return false;-}+ if (len == 0) {+ return base;+ }-/// Callers must have run rebuildIfDirty, which resets the memo.-fn visibilityProps(self: *StyleManager, el: *Element) Visibility.Computed {- return self.visibility.ownProps(self.arena.allocator(), el, self.frame);+ // The same path again, adding what each element inherits.+ var result = own;+ current = el;+ for (0..len) |distance| {+ const elem = current.?;+ var above = base;+ inline for (inherited) |field| {+ if (reach.get(@field(Inherited, field)) > distance) {+ @field(above, field) = true;+ }+ }+ if (group.memo.getPtr(elem)) |entry| {+ entry.* = entry.inherit(above);+ if (distance == 0) {+ result = entry.*;+ }+ } else if (distance == 0) {+ result = own.inherit(above);+ }+ current = elem.parentElement();+ }+ return result;}/// Callers must have run rebuildIfDirty, which resets the memo.@@ -736,14 +781,17 @@ fn Group(comptime Spec: type) type {return struct {const Self = @This();const Declared = Spec.Declared;+ // What we store in our memoconst Computed = Spec.Computed;+ // The element's own value, only used while resolving, never stored+ const Cascaded = if (@hasDecl(Spec, "Cascaded")) Spec.Cascaded else Computed;const Field = std.meta.FieldEnum(Declared);const fields = std.meta.fieldNames(Declared);comptime {- // compute copies each declared value into its Computed namesake+ // compute copies each declared value into its Cascaded namesakefor (fields) |field| {- std.debug.assert(@FieldType(Computed, field) == @typeInfo(@FieldType(Declared, field)).optional.child);+ std.debug.assert(@FieldType(Cascaded, field) == @typeInfo(@FieldType(Declared, field)).optional.child);}}@@ -770,7 +818,7 @@ fn Group(comptime Spec: type) type {// skip the cascade on the many pages without one.has_important: bool = false,- // Own-element results, valid while Page.style_version == memo_version.+ // Valid while Page.style_version == memo_version.memo: std.AutoHashMapUnmanaged(*Element, Computed) = .empty,memo_version: usize = 0,@@ -832,25 +880,31 @@ fn Group(comptime Spec: type) type {}fn ownProps(self: *Self, allocator: Allocator, el: *Element, frame: *Frame) Computed {+ comptime std.debug.assert(Cascaded == Computed);+ const gop = self.memoEntry(allocator, el, frame) orelse return self.compute(el, frame);+ if (gop.found_existing == false) {+ gop.value_ptr.* = self.compute(el, frame);+ }+ return gop.value_ptr.*;+ }++ // Null when the memo can't grow. A new entry's value is undefined, and+ // the pointer is invalidated by the next insert.+ fn memoEntry(self: *Self, allocator: Allocator, el: *Element, frame: *Frame) ?std.AutoHashMapUnmanaged(*Element, Computed).GetOrPutResult {const version = frame.page.style_version;if (self.memo_version != version) {self.memo.clearRetainingCapacity();self.memo_version = version;}- const gop = self.memo.getOrPut(allocator, el) catch |err| {+ return self.memo.getOrPut(allocator, el) catch |err| {log.warn(.browser, "StyleManager memo", .{ .err = err });- return self.compute(el, frame);+ return null;};- if (gop.found_existing) {- return gop.value_ptr.*;- }- gop.value_ptr.* = self.compute(el, frame);- return gop.value_ptr.*;}- fn compute(self: *const Self, el: *Element, frame: *Frame) Computed {- var p: Computed = .{};+ fn compute(self: *const Self, el: *Element, frame: *Frame) Cascaded {+ var p: Cascaded = .{};var priorities: Priorities(Declared) = .initFill(0);const inline_style = inlineDeclared(Declared, el, frame);@@ -879,7 +933,7 @@ fn Group(comptime Spec: type) type {if (self.has_important == false or declaresAny(declared) == false) {return false;}- var p: Computed = .{};+ var p: Cascaded = .{};var priorities: Priorities(Declared) = .initFill(INLINE_IMPORTANT_PRIORITY);inline for (fields) |field| {if (@field(declared, field) != null) {@@ -897,7 +951,7 @@ fn Group(comptime Spec: type) type {return false;}- fn applyRules(self: *const Self, p: *Computed, priorities: *Priorities(Declared), el: *Element, frame: *Frame) void {+ fn applyRules(self: *const Self, p: *Cascaded, priorities: *Priorities(Declared), el: *Element, frame: *Frame) void {if (el.getId()) |id| {if (self.id_rules.get(id)) |rules| {checkRules(&rules, p, priorities, el, frame);@@ -920,7 +974,7 @@ fn Group(comptime Spec: type) type {checkRules(&self.other_rules, p, priorities, el, frame);}- fn checkRules(rules: *const RuleList, p: *Computed, priorities: *Priorities(Declared), el: *Element, frame: *Frame) void {+ fn checkRules(rules: *const RuleList, p: *Cascaded, priorities: *Priorities(Declared), el: *Element, frame: *Frame) void {for (rules.items(.priority), rules.items(.props), rules.items(.selector)) |priority, rule, selector| {// Only rules that set a property nothing stronger has set yet are// worth matching.@@ -1255,7 +1309,8 @@ const Visibility = struct {}};- const Computed = packed struct(u6) {+ // The element's own values, from the cascade.+ const Cascaded = packed struct(u6) {// Author value (inline or sheet). Without `author_display` it's the UA// fallback: .none when matchesUaDisplayNoneRule, else .other.display: Display = .other,@@ -1263,26 +1318,51 @@ const Visibility = struct {visibility_hidden: bool = false,opacity_zero: bool = false,pointer_events_none: bool = false,+ };- fn probe(self: Computed, comptime what: Probe, options: CheckVisibilityOptions) bool {- return switch (what) {- .hidden => self.display == .none or- (options.check_visibility and self.visibility_hidden) or- (options.check_opacity and self.opacity_zero),- .visibility => self.visibility_hidden,- .pointer_events => self.pointer_events_none,+ const Computed = packed struct(u7) {+ display: Display = .other,+ author_display: bool = false,+ visibility_hidden: bool = false, // inherited+ pointer_events_none: bool = false, // inherited+ in_display_none: bool = false, // This element or an ancestor+ in_opacity_zero: bool = false, // This element or an ancestor++ // Set on an element when set on its parent+ const inherited = [_][]const u8{ "visibility_hidden", "pointer_events_none", "in_display_none", "in_opacity_zero" };++ fn fromOwn(own: Cascaded) Computed {+ return .{+ .display = own.display,+ .author_display = own.author_display,+ .visibility_hidden = own.visibility_hidden,+ .pointer_events_none = own.pointer_events_none,+ .in_display_none = own.display == .none,+ .in_opacity_zero = own.opacity_zero,};}- };- const Probe = enum { hidden, visibility, pointer_events };+ fn inherit(self: Computed, parent: Computed) Computed {+ var result = self;+ inline for (inherited) |field| {+ @field(result, field) = @field(self, field) or @field(parent, field);+ }+ return result;+ }++ fn isHidden(self: Computed, options: CheckVisibilityOptions) bool {+ return self.in_display_none or+ (options.check_visibility and self.visibility_hidden) or+ (options.check_opacity and self.in_opacity_zero);+ }+ };// UA stylesheet display:none fallback (HTML Rendering §15.3.1 "Hidden// elements"). Applied only when no author rule for `display` matched the// element — per CSS Cascade §6.1 any normal-origin author rule beats UA// origin regardless of specificity, so `.x { display: flex }` on a// `<div class="x" hidden>` must report visible.- fn finish(p: *Computed, el: *Element, priorities: *const Priorities(Declared)) void {+ fn finish(p: *Cascaded, el: *Element, priorities: *const Priorities(Declared)) void {p.author_display = priorities.get(.display) != 0;if (!p.author_display and matchesUaDisplayNoneRule(el)) {p.display = .none;@@ -1451,8 +1531,6 @@ const MAX_DOC_ORDER: u32 = std.math.maxInt(u22) - 1;const CheckVisibilityOptions = struct {check_visibility: bool = false,check_opacity: bool = false,- // false is only sound when every ancestor is already known visible.- ancestors: bool = true,};// Inline styles win over the stylesheets of the same importance, and the@@ -2047,6 +2125,102 @@ test "StyleManager: memo: reuse and invalidation" {try testing.expectEqual(2, sm.visibility.memo.count());}+test "StyleManager: memo: inherited values, any query order" {+ const frame = try testing.createFrame();+ defer testing.test_session.closeAllPages();+ const sm = &frame._style_manager;++ const div = try frame.window._document.createElement("div", null, frame);+ try Frame.parse.htmlAsChildren(frame, div.asNode(),+ \\<p><b style="pointer-events: none"><i style="visibility: hidden"><s style="opacity: 0"><u></u></s></i></b></p>+ );+ const p = div.asNode().firstChild().?.as(Element);+ const b = p.asNode().firstChild().?.as(Element);+ const i = b.asNode().firstChild().?.as(Element);+ const s = i.asNode().firstChild().?.as(Element);+ const u = s.asNode().firstChild().?.as(Element);++ // The deepest first stores every ancestor, each resolved against its parent+ try testing.expectEqual(true, sm.hasPointerEventsNone(u));+ try testing.expectEqual(6, sm.visibility.memo.count());+ try testing.expectEqual(Visibility.Computed{ .visibility_hidden = true, .pointer_events_none = true, .in_opacity_zero = true }, sm.visibility.memo.get(u).?);+ try testing.expectEqual(Visibility.Computed{ .visibility_hidden = true, .pointer_events_none = true }, sm.visibility.memo.get(i).?);+ try testing.expectEqual(Visibility.Computed{}, sm.visibility.memo.get(p).?);++ try testing.expectEqual(false, sm.isHidden(u, .{}));+ try testing.expectEqual(true, sm.isHidden(u, .{ .check_visibility = true }));+ try testing.expectEqual(true, sm.isHidden(u, .{ .check_opacity = true }));+ try testing.expectEqual(true, sm.isHidden(s, .{ .check_opacity = true }));+ try testing.expectEqual(false, sm.isHidden(i, .{ .check_opacity = true }));+ try testing.expectEqual(true, sm.hasVisibilityHiddenInherited(i));+ try testing.expectEqual(false, sm.hasVisibilityHiddenInherited(b));+ try testing.expectEqual(true, sm.hasPointerEventsNone(b));+ try testing.expectEqual(false, sm.hasPointerEventsNone(p));++ // A DOM change drops the memo; a middle element first, then below and above it+ try p.setStyle("display: none", frame);+ try testing.expectEqual(true, sm.isHidden(i, .{}));+ try testing.expectEqual(true, sm.isHidden(u, .{}));+ // display itself isn't inherited+ try testing.expectEqual(false, sm.hasDisplayNone(u));+ try testing.expectEqual(true, sm.hasDisplayNone(p));+ try testing.expectEqual(true, sm.hasPointerEventsNone(u));+ try testing.expectEqual(false, sm.hasPointerEventsNone(p));+ try testing.expectEqual(true, sm.isHidden(p, .{}));+ try testing.expectEqual(false, sm.isHidden(div, .{}));++ try p.setStyle("", frame);+ try testing.expectEqual(false, sm.isHidden(u, .{}));+ try testing.expectEqual(true, sm.hasPointerEventsNone(s));+}++// Every element of a deep chain probed in document order, the shape of an+// interactivity pass. Walking all ancestors per probe is quadratic, which only+// the growth between two depths a factor of TIMES apart tells apart.+test "StyleManager: memo: probing a deep tree stays linear" {+ const frame = try testing.createFrame();+ defer testing.test_session.closeAllPages();++ const SMALL = 1000;+ const TIMES = 10;+ // well above TIMES, well below TIMES squared+ const LIMIT = TIMES * 3;++ const small = try buildDivChain(frame, SMALL);+ const large = try buildDivChain(frame, SMALL * TIMES);++ const small_us = probeChain(frame, small);+ const large_us = probeChain(frame, large);+ const ratio = @as(f64, @floatFromInt(large_us)) / @as(f64, @floatFromInt(@max(small_us, 1)));+ try testing.expect(ratio < LIMIT);+}++fn buildDivChain(frame: *Frame, depth: usize) !*Element {+ const doc = frame.window._document;+ // Bottom-up: appending under a deep parent walks its ancestors+ var top = try doc.createElement("div", null, frame);+ try top.setStyle("pointer-events: none", frame);+ for (1..depth) |_| {+ const div = try doc.createElement("div", null, frame);+ _ = try div.asNode().appendChild(top.asNode(), frame);+ top = div;+ }+ return top;+}++fn probeChain(frame: *Frame, top: *Element) u64 {+ const sm = &frame._style_manager;+ const start = lp.datetime.microTimestamp(.awake);+ var current: ?*Element = top;+ while (current) |el| {+ const child = if (el.asNode().firstChild()) |c| c.as(Element) else null;+ // only the deepest one declares it+ std.debug.assert(sm.hasPointerEventsNone(el) == (child == null));+ current = child;+ }+ return @intCast(lp.datetime.microTimestamp(.awake) - start);+}+test "StyleManager: ruleInserted: append joins the buckets without a rebuild" {const frame = try testing.createFrame();defer testing.test_session.closeAllPages();diff --git a/src/server/cdp/AXNode.zig b/src/server/cdp/AXNode.zigindex b336bd9cf..687990c3e 100644--- a/src/server/cdp/AXNode.zig+++ b/src/server/cdp/AXNode.zig@@ -89,7 +89,7 @@ pub const Writer = struct {try self.walkQuery(self.root.dom, &ignore_cache, w);} else {const root = AXNode.fromNode(self.root.dom);- const root_hidden = if (self.root.dom.is(DOMNode.Element)) |el| isHidden(el, self.frame, .{}) else false;+ const root_hidden = if (self.root.dom.is(DOMNode.Element)) |el| isHidden(el, self.frame) else false;try self.writeTree(root, root_hidden, &ignore_cache, w);}return w.endArray();@@ -139,7 +139,7 @@ pub const Writer = struct {// visibility:hidden, aria-hidden, hidden, inert). Matches// Chromium: these elements aren't exposed to the AX tree.const el = dom_node.as(DOMNode.Element);- if (walker.inAriaHidden() or isHidden(el, self.frame, .{ .ancestors = false })) {+ if (walker.inAriaHidden() or isHidden(el, self.frame)) {continue;}},@@ -580,7 +580,7 @@ pub const Writer = struct {// Skip hidden element children so childIds matches the// subtree-pruning done in writeTree.if (child.is(DOMNode.Element)) |child_el| {- if (child_in_aria_hidden or isHidden(child_el, self.frame, .{ .ancestors = false })) {+ if (child_in_aria_hidden or isHidden(child_el, self.frame)) {continue;}}@@ -679,7 +679,7 @@ pub const Writer = struct {}const node = try self.registry.register(axn.dom);- const hidden = if (axn.dom.is(DOMNode.Element)) |el| isHidden(el, self.frame, .{}) else false;+ const hidden = if (axn.dom.is(DOMNode.Element)) |el| isHidden(el, self.frame) else false;const ignored = try axn.isIgnore(self.frame, in_aria_hidden, hidden, ignore_cache);try w.beginObject();@@ -1235,7 +1235,7 @@ fn labelPromotionTarget(// Only promote when the control is hidden; otherwise it appears// normally and the label stays as-is.- if (!isHidden(control, frame, .{})) return null;+ if (!isHidden(control, frame)) return null;if (control.getTag() != .input) return null;const input = control.as(DOMNode.Element.Html.Input);@@ -1291,18 +1291,13 @@ fn scratchAllocator(temp_arena: ?*lp.Arena, frame: *Frame) std.mem.Allocator {return if (temp_arena) |a| a.allocator() else frame.call_arena;}-const HiddenOptions = struct { ancestors: bool = true };-/// Chromium's AX tree prunes display:none and visibility:hidden alike.-fn isHidden(elt: *DOMNode.Element, frame: *Frame, options: HiddenOptions) bool {+fn isHidden(elt: *DOMNode.Element, frame: *Frame) bool {if (hasHidingAttribute(elt)) {return true;}const owner = elt.ownerFrame(frame) orelse return false;- return owner._style_manager.isHidden(elt, .{- .check_visibility = true,- .ancestors = options.ancestors,- });+ return owner._style_manager.isHidden(elt, .{ .check_visibility = true });}fn hasHidingAttribute(elt: *DOMNode.Element) bool {@@ -1369,7 +1364,7 @@ const IgnoreCache = struct {var tw = TreeWalker.FullExcludeSelf.init(root, .{});const exposed = while (tw.next()) |node| {- const node_hidden = if (node.is(DOMNode.Element)) |el| isHidden(el, frame, .{ .ancestors = false }) else false;+ const node_hidden = if (node.is(DOMNode.Element)) |el| isHidden(el, frame) else false;switch (AXNode.fromNode(node).ignoreSelf(false, node_hidden)) {.ignored => tw.skipChildren(),.exposed => break node,