perf: skip dedup map in FindNodes#576
Open
jvoisin wants to merge 1 commit into
Open
Conversation
FindNodes went through mapNodes, which builds a map[*html.Node]bool to
deduplicate results merged across source nodes, and wrapped every match in a
one-element []*html.Node slice. Both are wasteful here: each source node maps
to at most itself (when it is a descendant of the current selection), so
distinct source nodes can never collide. Append the matching nodes directly
into the result instead.
Duplicate *input* nodes still have to be discarded so the resulting Selection
stays a proper set, which mapNodes previously handled via
appendWithoutDuplicates. A cheap isInSlice check against the (small) result
slice preserves that, and is placed before the more expensive sliceContains
tree walk so duplicate inputs short-circuit early.
benchstat -count=12:
| old | new
| sec/op | sec/op vs base
FindNodes-8 | 232.6µ | 131.0µ -43.68% (p=0.000 n=12)
| old | new
| B/op | B/op vs base
FindNodes-8 | 12.023Ki | 2.164Ki -82.00% (p=0.000 n=12)
| old | new
| allocs/op| allocs/op vs base
FindNodes-8 | 85.000 | 9.000 -89.41% (p=0.000 n=12)
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.
FindNodes went through mapNodes, which builds a map[*html.Node]bool to deduplicate results merged across source nodes, and wrapped every match in a one-element []*html.Node slice. Both are wasteful here: each source node maps to at most itself (when it is a descendant of the current selection), so distinct source nodes can never collide. Append the matching nodes directly into the result instead.
Duplicate input nodes still have to be discarded so the resulting Selection stays a proper set, which mapNodes previously handled via appendWithoutDuplicates. A cheap isInSlice check against the (small) result slice preserves that, and is placed before the more expensive sliceContains tree walk so duplicate inputs short-circuit early.
benchstat -count=12:
FindNodes-8 | 232.6µ | 131.0µ -43.68% (p=0.000 n=12)
FindNodes-8 | 12.023Ki | 2.164Ki -82.00% (p=0.000 n=12)
FindNodes-8 | 85.000 | 9.000 -89.41% (p=0.000 n=12)