Skip to content

Read the last token of a selector that ends in a nested pseudo-class - #182

Merged
jdalton merged 1 commit into
dperini:masterfrom
jdalton:perf/optimizer-nesting
Sep 6, 2026
Merged

Read the last token of a selector that ends in a nested pseudo-class#182
jdalton merged 1 commit into
dperini:masterfrom
jdalton:perf/optimizer-nesting

Conversation

@jdalton

@jdalton jdalton commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Tooling update

Rebased onto merged #212 as one commit. Sources and tests use .mts, Node tests use Vitest, and the published JavaScript paths stay unchanged. The original engine changes are preserved.

Local lint, formatting, Node and packed-package checks pass. All 41 WPT pages pass against both generated builds.

Change

Read the final compound when a selector ends in nested functional pseudo-classes. This narrows the candidate list instead of scanning every element.

Extracted from #167. The parser variable is local, so strict factory initialization works without creating a global. Nesting beyond the pattern's supported depth retains the unoptimized fallback.

All six regression tests pass: strict initialization and five nested selector shapes, including cached queries. Run with Node.js ≥ 22 using pnpm install && pnpm run test:optimizer.

The original aggregate benchmark measured div:not(:nth-of-type(2n)) at 45.62 ms before and 134.92 µs after. Those historical timings were not rerun for this repair.

References: consume a simple block, logical combination pseudo-classes.

@jdalton
jdalton force-pushed the perf/optimizer-nesting branch 3 times, most recently from 14c3853 to ec74648 Compare September 5, 2026 18:56
@jdalton jdalton added the bug label Sep 5, 2026
@jdalton
jdalton force-pushed the perf/optimizer-nesting branch 2 times, most recently from 4678dac to 463fe40 Compare September 6, 2026 02:30
Before testing candidates, collect() asks reOptimizer for the last simple token of a selector and uses it to fetch the candidates by tag, class or id. The parenthesized part of that pattern is '\x28[^\x29]+(?:\x29|$)', which stops at the first ')', so a final compound holding a nested functional pseudo-class does not match at all — and a selector the optimizer cannot read is answered by walking every element in the context.
 'div:not(:nth-of-type(2n))' therefore tests every element in the document instead of the divs, and since ':not()' evaluates its argument through s.match() per element, each of those elements resolves nth-of-type. On a 6300-element page that is 6344 resolutions building 3911 sibling caches over 196312 steps, for a selector whose subject is a div.
 The parenthesized part now tolerates two levels of nesting, which reaches ':not(:not(:not(span)))'. Deeper than that falls back to the unoptimized scan, as before. Both the old and new patterns stay linear on unbalanced input: 3200 unclosed parentheses match in 0.02ms.
 div:not(:nth-of-type(2n)) 45.62ms -> 134.92us 338x div:not(:nth-child(3)) 9.42ms -> 123.94us 76x div:is(.example):not(:where(.x)) 2.65ms -> 39.45us 67x div:not(.x) 27.93us -> 27.75us -
 Results are unchanged; the four above agree with the native engine.

References:

- Spec: https://drafts.csswg.org/css-syntax/#consume-simple-block — why a parenthesized part has to tolerate nesting
- Spec: https://drafts.csswg.org/selectors-4/#matches — the functional pseudo-classes that put parentheses inside a compound
@jdalton
jdalton force-pushed the perf/optimizer-nesting branch from 463fe40 to 441874e Compare September 6, 2026 11:40
@jdalton
jdalton merged commit d63d634 into dperini:master Sep 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant