From da526bac92bb97c011127f4d38f63ca827ff124f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 19 Sep 2024 07:43:15 +0200 Subject: [PATCH] LibWeb: Don't invalidate layout tree on all DOM node removals DOM nodes that didn't have a layout node before being removed from the DOM are not going to change the shape of the layout tree after being removed. Observing this, we can avoid a full layout tree rebuild on some DOM node removals. This avoids a bunch of tree building work when loading https://x.com/ (cherry picked from commit 6f34758947852abae773798ba3ffa8e903fdc3d0) --- Userland/Libraries/LibWeb/DOM/Node.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index 70d9f37ee69..fb2df2383bb 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -714,6 +714,7 @@ WebIDL::ExceptionOr> Node::append_child(JS::NonnullGCPtr< void Node::remove(bool suppress_observers) { bool was_connected = is_connected(); + bool had_layout_node = layout_node(); // 1. Let parent be node’s parent auto* parent = this->parent(); @@ -855,7 +856,12 @@ void Node::remove(bool suppress_observers) // Since the tree structure has changed, we need to invalidate both style and layout. // In the future, we should find a way to only invalidate the parts that actually need it. document().invalidate_style(StyleInvalidationReason::NodeRemove); - document().invalidate_layout_tree(); + + // NOTE: If we didn't have a layout node before, rebuilding the layout tree isn't gonna give us one + // after we've been removed from the DOM. + if (had_layout_node) { + document().invalidate_layout_tree(); + } } document().bump_dom_tree_version();