Summary
Keys() and Values() start with nil and grow via append — bench.txt shows ~11 allocs/op at 1000 entries from slice regrowth alone. Map() similarly uses make(map[K]V) with no capacity. Pre-sizing with Len() halves those allocations at the cost of one extra O(n) traversal — a worthwhile trade-off, but only if documented so callers can make an informed choice.
Scope
Keys(): keys := make([]K, 0, m.Len())
Values(): values := make([]V, 0, m.Len())
Map(): out := make(map[K]V, m.Len())
- Godoc on each method notes that
Len() is called internally, doubling the traversal cost.
Acceptance criteria
- Benchmark numbers drop to ≤ 2 allocs/op for each method at 1000 entries.
bench.txt regenerated and committed.
make bench-regression green.
- Godoc documents the double-traversal trade-off.
Source: code-reviewer + performance-reviewer.
Summary
Keys()andValues()start withniland grow viaappend— bench.txt shows ~11 allocs/op at 1000 entries from slice regrowth alone.Map()similarly usesmake(map[K]V)with no capacity. Pre-sizing withLen()halves those allocations at the cost of one extra O(n) traversal — a worthwhile trade-off, but only if documented so callers can make an informed choice.Scope
Keys():keys := make([]K, 0, m.Len())Values():values := make([]V, 0, m.Len())Map():out := make(map[K]V, m.Len())Len()is called internally, doubling the traversal cost.Acceptance criteria
bench.txtregenerated and committed.make bench-regressiongreen.Source: code-reviewer + performance-reviewer.