Repository navigation
Limit how deep the rule index looks into :is() - #261
Merged
FlorianRappl merged 2 commits intoNov 2, 2026
Merged
Conversation
Indexing a rule for the cascade looks into the arguments of :is(), :where() and :matches(), parsing them again at every level, so the cost grows quadratically with the nesting: computing a style with 75 nested :is() took 45 us, 35 us with the cap (allocating 132 KB, 82 KB with it). - Look into at most 8 levels; a rule nested deeper stays ungrouped, i.e., it is a candidate for every element, which is always correct - Add tests for the boundary and for matching beyond it - Add DeepSelectorBenchmarks with 10, 50 and 75 levels
FlorianRappl
reviewed
Nov 2, 2026
FlorianRappl
left a comment
Contributor
There was a problem hiding this comment.
LGTM - please add the CHANGELOG entry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Types of Changes
Prerequisites
Please make sure you can check the following two boxes:
Contribution Type
What types of changes does your code introduce? Put an
xin all the boxes that apply:Description
A performance fix in the rule index of the cascade. There is no separate issue.
To group a rule by its subject,
StyleRuleIndexlooks into the arguments of:is(),:where()and:matches(). It parses the arguments' text again at every level, so a selector that nests them deeply costs quadratic time (and stack) to index, every time a document's rules are indexed.The change
StyleRuleIndexTests: 8 levels are still grouped and 9 are not (this case fails without the change), and a rule nested 9 deep still applies to its elements only.DeepSelectorBenchmarks: computing a style with a rule nesting:is()10, 50 and 75 levels deep. 75 is the limit AngleSharp #1402 proposes, so the shapes stay valid with it.Benchmarks
Paired builds of the same benchmark code against
devel(342fa84), alternating which goes first each round. BenchmarkDotNet 0.15.8, net10.0, AngleSharp 1.9.0 for both, pinned to the performance cores of an Intel Core Ultra 7 165H. Median of 3 rounds:DeepSelectorBenchmarks, 10 levelsDeepSelectorBenchmarks, 50 levelsDeepSelectorBenchmarks, 75 levelsCssCascadeBenchmarks.ComputedStyleCssCascadeBenchmarks.RenderTreeThe gain is modest: AngleSharp 1.9.0 already made the selector parsing behind it much cheaper (with AngleSharp 1.5.0, 50 levels took 442 μs on
devel). The 10-level row changes sign between rounds, and single rounds vary by up to ±5% on this machine; real style sheets are unaffected.Suggested CHANGELOG line
I left
CHANGELOG.mdout so you can word it; suggestion::is(),:where(), or:matches()(#PR) @michaelthyregod🤖 Generated with Claude Code