perf: short-circuit IsMatcher on the first match#559
Merged
Conversation
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%
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.
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%