Optimize single-rule CSS selector matching:

Add a fast path that directly evaluates selectors containing one rule,
  avoiding combinator traversal and switch overhead for common class, ID,
  tag, and pseudo-class selectors.

  Move complex selector traversal into a dedicated function and cache rule
  counts in selector loops. Add focused matching coverage and document the
  Phase 7 benchmark results.
This commit is contained in:
Martín Lucas Golini
2026-07-10 19:51:05 -03:00
parent d29dbb1cc1
commit 660d64c823
4 changed files with 55 additions and 7 deletions
@@ -828,6 +828,19 @@ Use instrumentation from Phase 4 to confirm:
# Phase 7: Add Single-Rule Selector Fast Path
**Status: Implemented**
## Implementation State
- `StyleSheetSelector` records whether parsing produced exactly one rule.
- Single-rule selectors call `StyleSheetSelectorRule::matches()` directly, while selectors with
combinators retain the existing traversal logic in `selectComplex()`.
- A focused test covers class, ID, tag+class, and pseudo-class selectors with pseudo matching both
enabled and disabled.
- Across seven release benchmark runs, `Benchmark.CSSSelectorMatching` had a 93.0 ms median for
20.48 million selector calls. Compared with the earlier Phase 5 median of approximately 111.4 ms,
this is an indicative 16.6% reduction.
## Motivation
Many selectors are simple: