Expand description
Epoch-scoped cache of CSS selector match results.
Selector matching is the dominant per-element cost after the rule set has
been built: every layout run walks the candidate rules of every element and
runs ComplexSelector::matches again, even though the result only depends on
the (immutable) DOM snapshot and the (immutable) rule set. Repeated layout
runs over an unchanged document — scroll, resize, hover and animation frames
— therefore repeat exactly the same matches.
SelectorMatchCache memoizes (selector, element) pairs. The key is the
identity of the ComplexSelector allocation (Arc::as_ptr) rather than
the selector text: rules sharing a selector group share one Arc, so the
pointer is stable for as long as the owning [RuleSet] is reused, and this
avoids hashing selector trees on every lookup.
§Invalidation contract
The cache is scoped to one DOM-snapshot/rule-set/interaction epoch.
SelectorMatchCache::clear MUST be called whenever any input that can
affect selector matching changes:
- The rule set is rebuilt (new selector allocations).
- The DOM snapshot changes (
dom_versionbumps): structural changes, attribute changes, pseudo-class state changes (:checked, :focus, etc.). - Interaction state changes (
interaction_versionbumps): :hover, :active, :focus-within, etc.
Entries carry the epoch they were created in. SelectorMatchCache::get_or_insert
returns a miss for entries from a previous epoch, so even if clear is
accidentally skipped, stale entries are never reused. clear also drops the
backing map to bound memory growth.
SelectorMatchCache is single-threaded: it is owned by a layout worker
and accessed only from that worker’s thread.
Structs§
- Selector
Match Cache - Memoizes selector match results for one DOM/rule-set epoch.
Constants§
- DEFAULT_
CAPACITY - Default number of memoized
(selector, element)results.