Skip to content

perf: skip dedup map in getChildrenNodes#570

Open
jvoisin wants to merge 1 commit into
PuerkitoBio:masterfrom
jvoisin:skipmap
Open

perf: skip dedup map in getChildrenNodes#570
jvoisin wants to merge 1 commit into
PuerkitoBio:masterfrom
jvoisin:skipmap

Conversation

@jvoisin

@jvoisin jvoisin commented May 30, 2026

Copy link
Copy Markdown
Contributor

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)

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)
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.

1 participant