perf: skip dedup map in getChildrenNodes#570
Open
jvoisin wants to merge 1 commit into
Open
Conversation
getChildrenNodes went through mapNodes, which builds a
map[*html.Node]bool to deduplicate results merged across source nodes.
That dedup is pointless in this case: every child has exactly one parent, so
the children of distinct (already-unique) source nodes form disjoint sets and
can never collide. Walk the sources directly into the result instead, keeping a
single-node fast path that returns the child slice without an extra copy.
This mirrors the disjoint-set reasoning already applied to the parent
traversal helpers.
benchstat -count=10:
sec/op B/op allocs/op
Contents-8 -55.52% -67.63% -19.23%
ContentsFiltered-8 -15.25% -33.61% -9.68%
ChildrenFiltered-8 -32.84% -3.23% -7.69%
Children-8 ~ (single-node fast path unchanged)
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.
getChildrenNodes went through mapNodes, which builds a map[*html.Node]bool to deduplicate results merged across source nodes. That dedup is pointless in this case: every child has exactly one parent, so the children of distinct (already-unique) source nodes form disjoint sets and can never collide. Walk the sources directly into the result instead, keeping a single-node fast path that returns the child slice without an extra copy.
This mirrors the disjoint-set reasoning already applied to the parent traversal helpers.
benchstat -count=10:
Contents-8 -55.52% -67.63% -19.23%
ContentsFiltered-8 -15.25% -33.61% -9.68%
ChildrenFiltered-8 -32.84% -3.23% -7.69%
Children-8 ~ (single-node fast path unchanged)