Skip to content

perf: skip dedup map in mapNodes for single-node selections#550

Merged
mna merged 1 commit into
PuerkitoBio:masterfrom
jvoisin:nomap
May 28, 2026
Merged

perf: skip dedup map in mapNodes for single-node selections#550
mna merged 1 commit into
PuerkitoBio:masterfrom
jvoisin:nomap

Conversation

@jvoisin
Copy link
Copy Markdown
Contributor

@jvoisin jvoisin commented May 23, 2026

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%

@mna
Copy link
Copy Markdown
Member

mna commented May 27, 2026

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%
@jvoisin
Copy link
Copy Markdown
Contributor Author

jvoisin commented May 27, 2026

Sure

@mna mna merged commit c38303d into PuerkitoBio:master May 28, 2026
10 of 12 checks passed
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.

2 participants