Skip to content

feat: pre-allocate Keys/Values/Map slices using Len #53

@millerjp

Description

@millerjp

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

  1. Benchmark numbers drop to ≤ 2 allocs/op for each method at 1000 entries.
  2. bench.txt regenerated and committed.
  3. make bench-regression green.
  4. Godoc documents the double-traversal trade-off.

Source: code-reviewer + performance-reviewer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Important, not blockingenhancementNew feature or requestperformancePerformance-related change

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions