LibWeb: Limit sibling style invalidation by max distance

If an element is affected only by selectors using the direct sibling
combinator `+`, we can calculate the maximum invalidation distance and
use it to limit style invalidation. For example, the selector
`.a + .b + .c` has a maximum invalidation distance of 2, meaning we can
skip invalidating any element affected by this selector if it's more
than two siblings away from the element that triggered the style
invalidation.

This change results in visible performance improvement when hovering
PR list on GitHub.
This commit is contained in:
Aliaksandr Kalenik 2025-03-10 16:16:08 +01:00 committed by Alexander Kalenik
parent 46abdd1126
commit 84ecaaa75c
Notes: github-actions[bot] 2025-03-10 17:57:49 +00:00
6 changed files with 61 additions and 10 deletions

View file

@ -460,16 +460,21 @@ void Node::invalidate_style(StyleInvalidationReason reason)
}
}
size_t current_sibling_distance = 1;
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
if (auto* element = as_if<Element>(sibling)) {
bool needs_to_invalidate = false;
if (reason == StyleInvalidationReason::NodeInsertBefore || reason == StyleInvalidationReason::NodeRemove) {
needs_to_invalidate = element->style_affected_by_structural_changes();
} else {
needs_to_invalidate = element->affected_by_sibling_combinator() || element->affected_by_nth_child_pseudo_class();
} else if (element->affected_by_indirect_sibling_combinator() || element->affected_by_nth_child_pseudo_class()) {
needs_to_invalidate = true;
} else if (element->affected_by_direct_sibling_combinator() && current_sibling_distance <= element->sibling_invalidation_distance()) {
needs_to_invalidate = true;
}
if (needs_to_invalidate)
if (needs_to_invalidate) {
element->set_entire_subtree_needs_style_update(true);
}
current_sibling_distance++;
}
}