LibWeb: Don't filter CSS rules into separate list based on @namespace

Instead, perform the filtering for each rule as we go. This avoids
creating a separate list of rules, which was ~5% of runtime when
mousing around on the Discord web interface.
This commit is contained in:
Andreas Kling 2023-08-03 14:08:02 +02:00
parent bb39ca9b4a
commit cf9565551a
Notes: sideshowbarker 2024-07-16 22:16:50 +09:00
2 changed files with 13 additions and 22 deletions

View file

@ -220,23 +220,14 @@ StyleComputer::RuleCache const& StyleComputer::rule_cache_for_cascade_origin(Cas
}
}
Vector<MatchingRule> StyleComputer::filter_namespace_rules(DOM::Element const& element, Vector<MatchingRule> const& rules) const
[[nodiscard]] static bool filter_namespace_rule(DOM::Element const& element, MatchingRule const& rule)
{
Vector<MatchingRule> filtered_rules;
for (auto const& rule : rules) {
auto namespace_uri = rule.sheet->default_namespace();
if (namespace_uri.has_value()) {
if (namespace_uri.value() == element.namespace_uri())
filtered_rules.append(rule);
} else {
filtered_rules.append(rule);
}
}
// FIXME: Filter out non-default namespace using prefixes
return filtered_rules;
auto namespace_uri = rule.sheet->default_namespace();
if (namespace_uri.has_value() && namespace_uri.value() != element.namespace_uri()) {
return false;
}
return true;
}
Vector<MatchingRule> StyleComputer::collect_matching_rules(DOM::Element const& element, CascadeOrigin cascade_origin, Optional<CSS::Selector::PseudoElement> pseudo_element) const
@ -245,15 +236,17 @@ Vector<MatchingRule> StyleComputer::collect_matching_rules(DOM::Element const& e
Vector<MatchingRule> rules_to_run;
auto add_rules_to_run = [&](Vector<MatchingRule> const& rules) {
Vector<MatchingRule> namespace_filtered_rules = filter_namespace_rules(element, rules);
rules_to_run.grow_capacity(rules_to_run.size() + rules.size());
if (pseudo_element.has_value()) {
for (auto& rule : namespace_filtered_rules) {
if (rule.contains_pseudo_element)
for (auto const& rule : rules) {
if (rule.contains_pseudo_element && filter_namespace_rule(element, rule))
rules_to_run.append(rule);
}
} else {
rules_to_run.extend(namespace_filtered_rules);
for (auto const& rule : rules) {
if (filter_namespace_rule(element, rule))
rules_to_run.append(rule);
}
}
};

View file

@ -174,8 +174,6 @@ private:
void build_rule_cache();
void build_rule_cache_if_needed() const;
Vector<MatchingRule> filter_namespace_rules(DOM::Element const&, Vector<MatchingRule> const&) const;
JS::NonnullGCPtr<DOM::Document> m_document;
struct AnimationKeyFrameSet {