perf: presize dedup map in mapNodes#562
Closed
jvoisin wants to merge 1 commit into
Closed
Conversation
mapNodes is the shared engine behind most traversal helpers (Parents, Children, Next/Prev, Siblings, Find*, Contents, ...). It allocates a map[*html.Node]bool used by appendWithoutDuplicates to skip duplicates across per-node results. The map was created with no size hint, so it incurred grow-and-double rehashing as it filled up. Pass len(nodes) as the size hint. That's a reasonable upper bound for the common case where each input yields 0 or 1 results (Parent, Next, Prev, Children, ...) and stays close enough for the multi-result cases (Parents, NextAll, PrevAll, ...) to still avoid most rehashes. Benchmarks (count=10, benchtime=500ms, linux/arm64): Notable per-bench wins (all p<0.01): Children -32.35% sec/op Prev -36.73% sec/op Next -29.76% sec/op NextFiltered -29.57% sec/op PrevFiltered -27.12% sec/op ParentsFilteredUntil* -25% sec/op, -21% B/op FindWithinSelection -17.97% sec/op, -21% B/op ContentsFiltered -20.34% sec/op NextUntil -16.25% sec/op ParentsUntil -15.42% sec/op Contents -12.09% sec/op Find -11.52% sec/op FindSelection / FindNodes feed mapNodes a 448-element input, so the presize bumps B/op by ~5 KiB on those two benchmarks — but allocs/op drops from 91 to 85, because the single sized allocation replaces the grow-and-double sequence the map went through before. No benchmark regressions were found.
Contributor
Author
|
Superseded by #563 |
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.
mapNodes is the shared engine behind most traversal helpers (Parents, Children, Next/Prev, Siblings, Find*, Contents, ...). It allocates a map[*html.Node]bool used by appendWithoutDuplicates to skip duplicates across per-node results. The map was created with no size hint, so it incurred grow-and-double rehashing as it filled up.
Pass len(nodes) as the size hint. That's a reasonable upper bound for the common case where each input yields 0 or 1 results (Parent, Next, Prev, Children, ...) and stays close enough for the multi-result cases (Parents, NextAll, PrevAll, ...) to still avoid most rehashes.
Benchmarks (count=10, benchtime=500ms, linux/arm64):
Notable per-bench wins (all p<0.01):
Children -32.35% sec/op
Prev -36.73% sec/op
Next -29.76% sec/op
NextFiltered -29.57% sec/op
PrevFiltered -27.12% sec/op
ParentsFilteredUntil* -25% sec/op, -21% B/op
FindWithinSelection -17.97% sec/op, -21% B/op
ContentsFiltered -20.34% sec/op
NextUntil -16.25% sec/op
ParentsUntil -15.42% sec/op
Contents -12.09% sec/op
Find -11.52% sec/op
FindSelection / FindNodes feed mapNodes a 448-element input, so the presize bumps B/op by ~5 KiB on those two benchmarks — but allocs/op drops from 91 to 85, because the single sized allocation replaces the grow-and-double sequence the map went through before.
No benchmark regressions were found.