1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:48:14 +00:00

LibWeb: Cache CSS rules in buckets to reduce number of rules checked

This patch introduces the StyleComputer::RuleCache, which divides all of
our (author) CSS rules into buckets.

Currently, there are two buckets:
- Rules where a specific class must be present.
- All other rules.

This allows us to check a significantly smaller set of rules for each
element, since we can skip over any rule that requires a class attribute
not present on the element.

This takes the typical numer of rules tested per element on Discord from
~16000 to ~550. :^)

We can definitely improve the cache invalidation. It currently happens
too often due to media queries. And we also need to make sure we
invalidate when mutating style through CSSOM APIs.
This commit is contained in:
Andreas Kling 2022-02-10 17:49:50 +01:00
parent 8d104b7de2
commit 646b37d1a9
5 changed files with 108 additions and 2 deletions

View file

@ -66,6 +66,8 @@ public:
Vector<MatchingRule> collect_matching_rules(DOM::Element const&, CascadeOrigin = CascadeOrigin::Any) const;
void invalidate_rule_cache();
private:
void compute_cascaded_values(StyleProperties&, DOM::Element&) const;
void compute_font(StyleProperties&, DOM::Element const*) const;
@ -88,7 +90,17 @@ private:
void cascade_declarations(StyleProperties&, DOM::Element&, Vector<MatchingRule> const&, CascadeOrigin, bool important, HashMap<String, StyleProperty const*> const&) const;
void build_rule_cache();
void build_rule_cache_if_needed() const;
DOM::Document& m_document;
struct RuleCache {
HashMap<FlyString, Vector<MatchingRule>> rules_by_class;
Vector<MatchingRule> other_rules;
int generation { 0 };
};
OwnPtr<RuleCache> m_rule_cache;
};
}