mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 01:00:05 +00:00
LibWeb: Add direct pointer to CSS::Selector in MatchingRule struct
This avoids looking up the selector by index repeatedly, giving us a ~400ms reduction in load time on https://wpt.fyi/
This commit is contained in:
parent
c3680a02a7
commit
e03aedbdf0
Notes:
github-actions[bot]
2025-01-26 14:08:36 +00:00
Author: https://github.com/awesomekling
Commit: e03aedbdf0
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3370
Reviewed-by: https://github.com/gmta ✅
3 changed files with 10 additions and 13 deletions
|
@ -503,8 +503,7 @@ Vector<MatchingRule const*> StyleComputer::collect_matching_rules(DOM::Element c
|
||||||
if (!rule_is_relevant_for_current_scope)
|
if (!rule_is_relevant_for_current_scope)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto const& selector = rule_to_run.absolutized_selectors()[rule_to_run.selector_index];
|
if (should_reject_with_ancestor_filter(rule_to_run.selector))
|
||||||
if (should_reject_with_ancestor_filter(*selector))
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
rules_to_run.unchecked_append(rule_to_run);
|
rules_to_run.unchecked_append(rule_to_run);
|
||||||
|
@ -566,7 +565,7 @@ Vector<MatchingRule const*> StyleComputer::collect_matching_rules(DOM::Element c
|
||||||
if (element.is_shadow_host() && rule_root != element.shadow_root())
|
if (element.is_shadow_host() && rule_root != element.shadow_root())
|
||||||
shadow_host_to_use = nullptr;
|
shadow_host_to_use = nullptr;
|
||||||
|
|
||||||
auto const& selector = rule_to_run.absolutized_selectors()[rule_to_run.selector_index];
|
auto const& selector = rule_to_run.selector;
|
||||||
|
|
||||||
SelectorEngine::MatchContext context { .style_sheet_for_rule = *rule_to_run.sheet };
|
SelectorEngine::MatchContext context { .style_sheet_for_rule = *rule_to_run.sheet };
|
||||||
ScopeGuard guard = [&] {
|
ScopeGuard guard = [&] {
|
||||||
|
@ -582,16 +581,17 @@ Vector<MatchingRule const*> StyleComputer::collect_matching_rules(DOM::Element c
|
||||||
}
|
}
|
||||||
matching_rules.append(&rule_to_run);
|
matching_rules.append(&rule_to_run);
|
||||||
}
|
}
|
||||||
|
|
||||||
return matching_rules;
|
return matching_rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sort_matching_rules(Vector<MatchingRule const*>& matching_rules)
|
static void sort_matching_rules(Vector<MatchingRule const*>& matching_rules)
|
||||||
{
|
{
|
||||||
quick_sort(matching_rules, [&](MatchingRule const* a, MatchingRule const* b) {
|
quick_sort(matching_rules, [&](MatchingRule const* a, MatchingRule const* b) {
|
||||||
auto const& a_selector = a->absolutized_selectors()[a->selector_index];
|
auto const& a_selector = a->selector;
|
||||||
auto const& b_selector = b->absolutized_selectors()[b->selector_index];
|
auto const& b_selector = b->selector;
|
||||||
auto a_specificity = a_selector->specificity();
|
auto a_specificity = a_selector.specificity();
|
||||||
auto b_specificity = b_selector->specificity();
|
auto b_specificity = b_selector.specificity();
|
||||||
if (a_specificity == b_specificity) {
|
if (a_specificity == b_specificity) {
|
||||||
if (a->style_sheet_index == b->style_sheet_index)
|
if (a->style_sheet_index == b->style_sheet_index)
|
||||||
return a->rule_index < b->rule_index;
|
return a->rule_index < b->rule_index;
|
||||||
|
@ -2599,7 +2599,6 @@ StyleComputer::BuiltRuleCaches StyleComputer::make_rule_cache_for_cascade_origin
|
||||||
for_each_stylesheet(cascade_origin, [&](auto& sheet, GC::Ptr<DOM::ShadowRoot> shadow_root) {
|
for_each_stylesheet(cascade_origin, [&](auto& sheet, GC::Ptr<DOM::ShadowRoot> shadow_root) {
|
||||||
size_t rule_index = 0;
|
size_t rule_index = 0;
|
||||||
sheet.for_each_effective_style_producing_rule([&](auto const& rule) {
|
sheet.for_each_effective_style_producing_rule([&](auto const& rule) {
|
||||||
size_t selector_index = 0;
|
|
||||||
SelectorList const& absolutized_selectors = [&]() {
|
SelectorList const& absolutized_selectors = [&]() {
|
||||||
if (rule.type() == CSSRule::Type::Style)
|
if (rule.type() == CSSRule::Type::Style)
|
||||||
return static_cast<CSSStyleRule const&>(rule).absolutized_selectors();
|
return static_cast<CSSStyleRule const&>(rule).absolutized_selectors();
|
||||||
|
@ -2617,9 +2616,9 @@ StyleComputer::BuiltRuleCaches StyleComputer::make_rule_cache_for_cascade_origin
|
||||||
shadow_root,
|
shadow_root,
|
||||||
&rule,
|
&rule,
|
||||||
sheet,
|
sheet,
|
||||||
|
selector,
|
||||||
style_sheet_index,
|
style_sheet_index,
|
||||||
rule_index,
|
rule_index,
|
||||||
selector_index,
|
|
||||||
selector.specificity(),
|
selector.specificity(),
|
||||||
cascade_origin,
|
cascade_origin,
|
||||||
false,
|
false,
|
||||||
|
@ -2751,8 +2750,6 @@ StyleComputer::BuiltRuleCaches StyleComputer::make_rule_cache_for_cascade_origin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
++selector_index;
|
|
||||||
}
|
}
|
||||||
++rule_index;
|
++rule_index;
|
||||||
});
|
});
|
||||||
|
|
|
@ -78,9 +78,9 @@ struct MatchingRule {
|
||||||
GC::Ptr<DOM::ShadowRoot const> shadow_root;
|
GC::Ptr<DOM::ShadowRoot const> shadow_root;
|
||||||
GC::Ptr<CSSRule const> rule; // Either CSSStyleRule or CSSNestedDeclarations
|
GC::Ptr<CSSRule const> rule; // Either CSSStyleRule or CSSNestedDeclarations
|
||||||
GC::Ptr<CSSStyleSheet const> sheet;
|
GC::Ptr<CSSStyleSheet const> sheet;
|
||||||
|
Selector const& selector;
|
||||||
size_t style_sheet_index { 0 };
|
size_t style_sheet_index { 0 };
|
||||||
size_t rule_index { 0 };
|
size_t rule_index { 0 };
|
||||||
size_t selector_index { 0 };
|
|
||||||
|
|
||||||
u32 specificity { 0 };
|
u32 specificity { 0 };
|
||||||
CascadeOrigin cascade_origin;
|
CascadeOrigin cascade_origin;
|
||||||
|
|
|
@ -1652,7 +1652,7 @@ void Document::invalidate_style_for_elements_affected_by_hover_change(Node& old_
|
||||||
if (!rule_is_relevant_for_current_scope)
|
if (!rule_is_relevant_for_current_scope)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
auto const& selector = rule.absolutized_selectors()[rule.selector_index];
|
auto const& selector = rule.selector;
|
||||||
|
|
||||||
SelectorEngine::MatchContext context;
|
SelectorEngine::MatchContext context;
|
||||||
bool selector_matched = false;
|
bool selector_matched = false;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue