Skip to content

perf: probe subtrees directly in Has / HasMatcher#564

Merged
mna merged 1 commit into
PuerkitoBio:masterfrom
jvoisin:hasm
May 29, 2026
Merged

perf: probe subtrees directly in Has / HasMatcher#564
mna merged 1 commit into
PuerkitoBio:masterfrom
jvoisin:hasm

Conversation

@jvoisin
Copy link
Copy Markdown
Contributor

@jvoisin jvoisin commented May 29, 2026

Has(selector) and HasMatcher(m) used to call
s.document.Find(selector)/FindMatcher(m), materializing every matching descendant in the entire document, then iterate s.Nodes x matchedNodes x treeDepth via nodeContains to keep the elements that contain one.

This commit probes each selection node's subtree directly with SingleMatcher, which short-circuits on the first descendant match.

BenchmarkHas (DocW().Find("h2").Has(".editsection")), -count=10:

           sec/op       B/op
before     61.97µs       744 B
after       3.33µs       360 B
            -94.6%      -51.6%

Has(selector) and HasMatcher(m) used to call
s.document.Find(selector)/FindMatcher(m), materializing every matching
descendant in the entire document, then iterate s.Nodes x matchedNodes x
treeDepth via nodeContains to keep the elements that contain one.

This commit probes each selection node's subtree directly with SingleMatcher, which
short-circuits on the first descendant match.

BenchmarkHas (DocW().Find("h2").Has(".editsection")), -count=10:

               sec/op       B/op
    before     61.97µs       744 B
    after       3.33µs       360 B
                -94.6%      -51.6%
@mna mna merged commit fa0370e into PuerkitoBio:master May 29, 2026
6 checks passed
@jvoisin jvoisin deleted the hasm branch May 29, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants