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/
This commit is contained in:
Andreas Kling 2024-09-19 07:43:15 +02:00 committed by Andreas Kling
parent aa8f17aea4
commit 6f34758947

View File

@ -714,6 +714,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> 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 nodes 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();