Skip to content

perf: short-circuit IsMatcher on the first match#559

Merged
mna merged 1 commit into
PuerkitoBio:masterfrom
jvoisin:is-matcher-early-return
May 28, 2026
Merged

perf: short-circuit IsMatcher on the first match#559
mna merged 1 commit into
PuerkitoBio:masterfrom
jvoisin:is-matcher-early-return

Conversation

@jvoisin
Copy link
Copy Markdown
Contributor

@jvoisin jvoisin commented May 27, 2026

IsMatcher handled multi-node selections by calling m.Filter(s.Nodes) and checking whether the returned slice was non-empty, forcing the matcher to walk the whole selection and materialize all matches even though the API only needs an existence check.

This commit replaces this with a simple loop over s.Nodes that calls m.Match(n) and returns true on the first match. This also makes the single-node special case unnecessary.

Benchmark results on arm64 over 10 runs:

name old time/op new time/op delta
Is-8 4.67µs 1.32µs -71.7%
IsPositional-8 21.44µs 0.82µs -96.2%

name old B/op new B/op delta
Is-8 96 80 -16.7%
IsPositional-8 1144 184 -83.9%

name old allocs new allocs delta
Is-8 5 4 -20.0%
IsPositional-8 11 7 -36.4%

IsMatcher handled multi-node selections by calling m.Filter(s.Nodes) and
checking whether the returned slice was non-empty, forcing the matcher to walk
the whole selection and materialize all matches even though the API only needs
an existence check.

This commit replaces this with a simple loop over s.Nodes that calls m.Match(n)
and returns true on the first match. This also makes the single-node special
case unnecessary.

Benchmark results on arm64 over 10 runs:

name            old time/op   new time/op   delta
Is-8             4.67µs        1.32µs       -71.7%
IsPositional-8   21.44µs       0.82µs       -96.2%

name            old B/op      new B/op      delta
Is-8             96           80           -16.7%
IsPositional-8   1144         184          -83.9%

name            old allocs    new allocs    delta
Is-8             5            4            -20.0%
IsPositional-8   11           7            -36.4%
@mna mna merged commit 69b9b29 into PuerkitoBio:master May 28, 2026
6 checks passed
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