mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-06 08:09:45 +00:00
LibWeb: Invalidate less elements affected by CSS custom properties
Before this change, whenever element's attributes changed, we would add a flag to "pending invalidation", indicating that all descendants whose style uses CSS custom properties needed to be recomputed. This resulted in severe overinvalidation, because we would run invalidation regardless of whether any custom property on affected element actually changed. This change takes another approach, and now we decide whether descendant's style needs to be recomputed based on whether ancestor's style recomputation results in a change of custom properties, though this approach adds a little overhead to style computation as now we have to compare old vs new hashmap of custom properties. This brings substantial improvement on discord and x.com where, before this change, advantage of using invalidation sets was lost and we had to recompute all descendants, because almost all of them use custom properties.
This commit is contained in:
parent
cbe4ba60c3
commit
d1fbb7b51e
Notes:
github-actions[bot]
2025-07-30 09:07:23 +00:00
Author: https://github.com/kalenikaliaksandr
Commit: d1fbb7b51e
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5618
Reviewed-by: https://github.com/awesomekling
Reviewed-by: https://github.com/tcl3
13 changed files with 71 additions and 67 deletions
|
@ -1427,7 +1427,7 @@ void Document::update_layout(UpdateLayoutReason reason)
|
|||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] static CSS::RequiredInvalidationAfterStyleChange update_style_recursively(Node& node, CSS::StyleComputer& style_computer, bool needs_inherited_style_update)
|
||||
[[nodiscard]] static CSS::RequiredInvalidationAfterStyleChange update_style_recursively(Node& node, CSS::StyleComputer& style_computer, bool needs_inherited_style_update, bool recompute_elements_depending_on_custom_properties)
|
||||
{
|
||||
bool const needs_full_style_update = node.document().needs_full_style_update();
|
||||
CSS::RequiredInvalidationAfterStyleChange invalidation;
|
||||
|
@ -1440,12 +1440,14 @@ void Document::update_layout(UpdateLayoutReason reason)
|
|||
// We will still recompute style for the children, though.
|
||||
bool is_display_none = false;
|
||||
|
||||
bool did_change_custom_properties = false;
|
||||
CSS::RequiredInvalidationAfterStyleChange node_invalidation;
|
||||
if (is<Element>(node)) {
|
||||
if (needs_full_style_update || node.needs_style_update()) {
|
||||
node_invalidation = static_cast<Element&>(node).recompute_style();
|
||||
auto& element = static_cast<Element&>(node);
|
||||
if (needs_full_style_update || node.needs_style_update() || (recompute_elements_depending_on_custom_properties && element.style_uses_var_css_function())) {
|
||||
node_invalidation = element.recompute_style(did_change_custom_properties);
|
||||
} else if (needs_inherited_style_update) {
|
||||
node_invalidation = static_cast<Element&>(node).recompute_inherited_style();
|
||||
node_invalidation = element.recompute_inherited_style();
|
||||
}
|
||||
is_display_none = static_cast<Element&>(node).computed_properties()->display().is_none();
|
||||
}
|
||||
|
@ -1464,12 +1466,16 @@ void Document::update_layout(UpdateLayoutReason reason)
|
|||
node.set_needs_style_update(false);
|
||||
invalidation |= node_invalidation;
|
||||
|
||||
if (did_change_custom_properties) {
|
||||
recompute_elements_depending_on_custom_properties = true;
|
||||
}
|
||||
|
||||
bool children_need_inherited_style_update = !invalidation.is_none();
|
||||
if (needs_full_style_update || node.child_needs_style_update() || children_need_inherited_style_update) {
|
||||
if (needs_full_style_update || node.child_needs_style_update() || children_need_inherited_style_update || recompute_elements_depending_on_custom_properties) {
|
||||
if (node.is_element()) {
|
||||
if (auto shadow_root = static_cast<DOM::Element&>(node).shadow_root()) {
|
||||
if (needs_full_style_update || shadow_root->needs_style_update() || shadow_root->child_needs_style_update()) {
|
||||
auto subtree_invalidation = update_style_recursively(*shadow_root, style_computer, children_need_inherited_style_update);
|
||||
auto subtree_invalidation = update_style_recursively(*shadow_root, style_computer, children_need_inherited_style_update, recompute_elements_depending_on_custom_properties);
|
||||
if (!is_display_none)
|
||||
invalidation |= subtree_invalidation;
|
||||
}
|
||||
|
@ -1477,8 +1483,8 @@ void Document::update_layout(UpdateLayoutReason reason)
|
|||
}
|
||||
|
||||
node.for_each_child([&](auto& child) {
|
||||
if (needs_full_style_update || child.needs_style_update() || children_need_inherited_style_update || child.child_needs_style_update()) {
|
||||
auto subtree_invalidation = update_style_recursively(child, style_computer, children_need_inherited_style_update);
|
||||
if (needs_full_style_update || child.needs_style_update() || children_need_inherited_style_update || child.child_needs_style_update() || recompute_elements_depending_on_custom_properties) {
|
||||
auto subtree_invalidation = update_style_recursively(child, style_computer, children_need_inherited_style_update, recompute_elements_depending_on_custom_properties);
|
||||
if (!is_display_none)
|
||||
invalidation |= subtree_invalidation;
|
||||
}
|
||||
|
@ -1523,7 +1529,7 @@ void Document::update_style()
|
|||
|
||||
style_computer().reset_ancestor_filter();
|
||||
|
||||
auto invalidation = update_style_recursively(*this, style_computer(), false);
|
||||
auto invalidation = update_style_recursively(*this, style_computer(), false, false);
|
||||
if (!invalidation.is_none())
|
||||
invalidate_display_list();
|
||||
if (invalidation.rebuild_stacking_context_tree)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue