Skip to content

perf: presize node-set maps in mapNodes, ClosestNodes and winnowNodes#563

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

perf: presize node-set maps in mapNodes, ClosestNodes and winnowNodes#563
jvoisin wants to merge 1 commit into
PuerkitoBio:masterfrom
jvoisin:precise

Conversation

@jvoisin
Copy link
Copy Markdown
Contributor

@jvoisin jvoisin commented May 27, 2026

Three internal helpers build a map[*html.Node]bool from a known set of nodes (or use one as a dedup accumulator over a known number of input nodes) but allocate the map without a size hint. They then incur grow-and-double rehashing as entries are inserted.

Pass len(nodes) as the size hint at make time:

  • traversal.go mapNodes: dedup map shared by appendWithoutDuplicates across every per-node result. mapNodes is the engine behind most traversal helpers (Parents, Children, Next/Prev, Siblings, Find*, Contents, ...), so the impact is broad.
  • traversal.go ClosestNodes: target-node set built from nodes...
  • filter.go winnowNodes: large-N path's lookup set built from nodes...

Double-digit performance gains across ~all benchmarks.

Three internal helpers build a map[*html.Node]bool from a known set of
nodes (or use one as a dedup accumulator over a known number of input
nodes) but allocate the map without a size hint. They then incur
grow-and-double rehashing as entries are inserted.

Pass len(nodes) as the size hint at make time:

- traversal.go mapNodes: dedup map shared by appendWithoutDuplicates
  across every per-node result. mapNodes is the engine behind most
  traversal helpers (Parents, Children, Next/Prev, Siblings, Find*,
  Contents, ...), so the impact is broad.
- traversal.go ClosestNodes: target-node set built from nodes...
- filter.go winnowNodes: large-N path's lookup set built from nodes...

Double-digit performance gains across ~all benchmarks.
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