perf: skip dedup map in mapNodes for single-node selections#550
Merged
Conversation
Member
|
LGTM but while we're adding this, let's handle len(nodes) == 0 too. |
mapNodes always allocated a map[*html.Node]bool for deduplication, even when called with a single source node where duplicates cannot arise. Short-circuit to return the callback result directly when len(nodes)==1. Also add a special case for when there are zero nodes. This benefits all traversal methods (Find, Children, Parent, Next, Prev, etc.) when operating on a single-node selection, which is the common case after First(), Eq(), or directly on a Document. name old ns/op new ns/op delta Find-8 23200 14200 -38.8% name old B/op new B/op delta Find-8 4632 1496 -67.7% name old allocs new allocs delta Find-8 27 13 -51.9%
Contributor
Author
|
Sure |
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.
mapNodes always allocated a map[*html.Node]bool for deduplication, even when called with a single source node where duplicates cannot arise. Short-circuit to return the callback result directly when len(nodes)==1.
This benefits all traversal methods (Find, Children, Parent, Next, Prev, etc.) when operating on a single-node selection, which is the common case after First(), Eq(), or directly on a Document.
name old ns/op new ns/op delta
Find-8 23200 14200 -38.8%
name old B/op new B/op delta
Find-8 4632 1496 -67.7%
name old allocs new allocs delta
Find-8 27 13 -51.9%