Skip to content

Truly independent web browser

LibWeb: Place runs of new siblings without rescanning the DOM

Andreas Kling4c6263942f4d
diff --git a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
index 46e161de44d..fb1378bbd3a 100644
--- a/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/tree_builder.rs
@@ -67,6 +67,8 @@ pub(crate) struct TreeBuilderState {
/// The document's style, which the build is handed before it starts when it may build the
/// viewport, held until the viewport's row takes it or the build ends without one.
pub(super) document_style: Option<DerivedStyleRecord>,
+ /// The run of new siblings the walk is placing in DOM order, if the last insertion started or continued one.
+ dom_order_run: Option<DomOrderRun>,
}
impl Default for TreeBuilderState {
@@ -88,6 +90,7 @@ impl Default for TreeBuilderState {
reports: Vec::new(),
pinned_style_records: Vec::new(),
document_style: None,
+ dom_order_run: None,
}
}
}
@@ -4769,8 +4772,55 @@ fn wrap_children_in_anonymous_wrapper(
}
}
+/// A run of new siblings that a walk places in DOM order. Every one of them goes before the same box: the first of the
+/// run looked past the siblings after it for a box of `parent`, and a walk reaches those siblings in DOM order, so none
+/// of the ones it looked past has gained such a box since. Without this, placing N new siblings looks past O(N) siblings
+/// N times, which stalls a large document that arrives in a few chunks.
+#[derive(Clone, Copy)]
+struct DomOrderRun {
+ parent: LayoutNode,
+ last_placed: StyleNodeID,
+ /// The sibling whose box the run goes before, where it goes before a sibling's box.
+ next_sibling_with_box: Option<StyleNodeID>,
+ before: LayoutNode,
+}
+
+impl DomOrderRun {
+ /// The siblings between two children of a run that have no box of their own, such as the white space between two
+ /// elements. A longer gap ends the run, which costs one more look past the siblings after it.
+ const MAX_SKIPPED_SIBLINGS: usize = 8;
+
+ fn continues_with(&self, host: &TreeBuilderHost<'_>, parent: LayoutNode, identity: StyleNodeID) -> bool {
+ // NB: The walk may have replaced the box, whose slot then holds nothing or another box.
+ if self.parent != parent
+ || (!self.before.is_invalid()
+ && (!host.arena().slot_is_live(self.before) || host.parent(self.before) != parent))
+ {
+ return false;
+ }
+ if identity == self.last_placed {
+ return true;
+ }
+ let mut sibling = self.last_placed;
+ for _ in 0..=Self::MAX_SKIPPED_SIBLINGS {
+ let Some(next) = host.next_dom_sibling(sibling) else {
+ return false;
+ };
+ if Some(next) == self.next_sibling_with_box {
+ return false;
+ }
+ if next == identity {
+ return true;
+ }
+ sibling = next;
+ }
+ false
+ }
+}
+
fn insert_child_in_dom_order(
host: &mut TreeBuilderHost<'_>,
+ state: &mut TreeBuilderState,
parent: LayoutNode,
child: UnplacedLayoutNode,
identity: StyleNodeID,
@@ -4799,12 +4849,36 @@ fn insert_child_in_dom_order(
}
}
+ if let Some(run) = state.dom_order_run.as_mut()
+ && run.continues_with(host, parent, identity)
+ {
+ run.last_placed = identity;
+ host.attach_child(parent, child, run.before);
+ return;
+ }
+
+ let (before, next_sibling_with_box) = box_to_place_dom_child_before(host, parent, identity);
+ state.dom_order_run = Some(DomOrderRun {
+ parent,
+ last_placed: identity,
+ next_sibling_with_box,
+ before,
+ });
+ host.attach_child(parent, child, before);
+}
+
+/// The child of `parent` that a new box of `identity` goes before, or an invalid node for the end of the child list,
+/// and the sibling of `identity` that child is the box of, if it is one.
+fn box_to_place_dom_child_before(
+ host: &TreeBuilderHost<'_>,
+ parent: LayoutNode,
+ identity: StyleNodeID,
+) -> (LayoutNode, Option<StyleNodeID>) {
let mut sibling = host.next_dom_sibling(identity);
while let Some(current) = sibling {
let sibling_layout_node = host.arena().bound_row(current);
if !sibling_layout_node.is_invalid() && host.parent(sibling_layout_node) == parent {
- host.attach_child(parent, child, sibling_layout_node);
- return;
+ return (sibling_layout_node, Some(current));
}
sibling = host.next_dom_sibling(current);
}
@@ -4816,20 +4890,18 @@ fn insert_child_in_dom_order(
after_layout_child = host.parent(after_layout_child);
}
if host.parent(after_layout_child) == parent {
- host.attach_child(parent, child, after_layout_child);
- return;
+ return (after_layout_child, None);
}
}
let mut layout_child = host.first_child(parent);
while !layout_child.is_invalid() {
if host.data(layout_child).generated_for.get() == GENERATED_FOR_AFTER {
- host.attach_child(parent, child, layout_child);
- return;
+ return (layout_child, None);
}
layout_child = host.next_sibling(layout_child);
}
- host.attach_child(parent, child, NodeSlotId::INVALID);
+ (NodeSlotId::INVALID, None)
}
/// Whether `node` is an absolutely positioned box that would be inline-level in flow, laid out among the inline content
@@ -4894,6 +4966,7 @@ fn insert_node_into_inline_or_block_ancestor(
}
FfiInsertionMode::InDomOrder => insert_child_in_dom_order(
host,
+ state,
insertion_point,
node,
identity.expect("an insertion in DOM order names its node"),
diff --git a/Tests/LibWeb/Layout/expected/block-and-inline/dom-order-insertion-run.txt b/Tests/LibWeb/Layout/expected/block-and-inline/dom-order-insertion-run.txt
new file mode 100644
index 00000000000..34d1a5e8c37
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/block-and-inline/dom-order-insertion-run.txt
@@ -0,0 +1,61 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 224 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 208 0+0+8] children: not-inline
+ BlockContainer <div#list> at [8,8] [0+0+0 784 0+0+0] [0+0+0 160 0+0+0] children: not-inline
+ BlockContainer <div#first> at [8,8] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [8,8 36.03125x16] baseline: 12.796875
+ "first"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,24] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,24 18.9375x16] baseline: 12.796875
+ "a0"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,40] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,40 15.6875x16] baseline: 12.796875
+ "a1"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,56] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,56 18.15625x16] baseline: 12.796875
+ "a2"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,72] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [8,72 9.46875x16] baseline: 12.796875
+ "b"
+ TextNode <#text> (not painted)
+ BlockContainer <div#last> at [8,88] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 4, rect: [8,88 29.78125x16] baseline: 12.796875
+ "last"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,104] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,104 18.484375x16] baseline: 12.796875
+ "c0"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,120] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,120 15.234375x16] baseline: 12.796875
+ "c1"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,136] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,136 17.703125x16] baseline: 12.796875
+ "c2"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,152] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ InlineNode <(anonymous)> at [8,152] [0+0+0 26.1875 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,152 26.1875x16] baseline: 12.796875
+ "end"
+ GeneratedTextNode <(anonymous)> (not painted)
+ BlockContainer <div#tail> at [8,168] [0+0+0 784 0+0+0] [0+0+0 48 0+0+0] children: not-inline
+ BlockContainer <div> at [8,168] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,168 17.453125x16] baseline: 12.796875
+ "d0"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,184] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,184 14.203125x16] baseline: 12.796875
+ "d1"
+ TextNode <#text> (not painted)
+ BlockContainer <div> at [8,200] [0+0+0 784 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 2, rect: [8,200 16.671875x16] baseline: 12.796875
+ "d2"
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x224] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/block-and-inline/dom-order-insertion-run.html b/Tests/LibWeb/Layout/input/block-and-inline/dom-order-insertion-run.html
new file mode 100644
index 00000000000..a002e097a38
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/block-and-inline/dom-order-insertion-run.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<style>
+ #list::after {
+ content: "end";
+ }
+</style>
+<div id="list">
+ <div id="first">first</div>
+ <div id="last">last</div>
+</div>
+<div id="tail"></div>
+<script>
+ document.body.offsetWidth;
+
+ const list = document.getElementById("list");
+ const last = document.getElementById("last");
+ const tail = document.getElementById("tail");
+
+ function block(text) {
+ const element = document.createElement("div");
+ element.textContent = text;
+ return element;
+ }
+
+ // A run of new siblings that goes before an existing box, with gaps of nodes that have no box of their own.
+ for (let i = 0; i < 3; ++i) {
+ list.insertBefore(block(`a${i}`), last);
+ list.insertBefore(document.createTextNode("\n"), last);
+ }
+ for (let i = 0; i < 12; ++i)
+ list.insertBefore(document.createComment("gap"), last);
+ list.insertBefore(block("b"), last);
+
+ // A run of new siblings that goes before the ::after box of its parent.
+ for (let i = 0; i < 3; ++i) {
+ list.appendChild(block(`c${i}`));
+ list.appendChild(document.createTextNode("\n"));
+ }
+
+ // A run of new siblings that goes at the end of a child list.
+ for (let i = 0; i < 3; ++i)
+ tail.appendChild(block(`d${i}`));
+</script>