Skip to content

Commit

Permalink
LibWeb: Don't invalidate layout tree on all DOM node removals
Browse files Browse the repository at this point in the history
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/
  • Loading branch information
awesomekling committed Sep 19, 2024
1 parent aa8f17a commit 6f34758
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Userland/Libraries/LibWeb/DOM/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 node’s parent
auto* parent = this->parent();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6f34758

Please sign in to comment.