From b365a5c42f4e978496fb03924b129406d4a6c5b0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 9 Sep 2024 10:51:39 +0200 Subject: [PATCH] LibWeb: Bucket div.foo and div#foo as class/ID rather than tag(div) By bucketing these seletors by class or ID, we can avoid running them in more cases. Before, we were only avoiding them if the context element wasn't a div. Now we avoid them for any element that doesn't have that specific class or ID. This reduces the number of selectors ran on https://vercel.com by a bit more, from 1.90% to 1.65%. --- Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index a53c0a55972..9d218f6a818 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -2700,8 +2700,10 @@ NonnullOwnPtr StyleComputer::make_rule_cache_for_casca } } + // NOTE: We traverse the simple selectors in reverse order to make sure that class/ID buckets are preferred over tag buckets + // in the common case of div.foo or div#foo selectors. bool added_to_bucket = false; - for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors) { + for (auto const& simple_selector : selector.compound_selectors().last().simple_selectors.in_reverse()) { if (simple_selector.type == CSS::Selector::SimpleSelector::Type::Id) { rule_cache->rules_by_id.ensure(simple_selector.name()).append(move(matching_rule)); ++num_id_rules;