Pre-allocates result slice with estimated capacity to reduce
repeated allocations during append operations.
Strategy:
- Estimates capacity as len(values) * 2
- Assumes average of 2-3 items per mapped element
- Simple heuristic that works well for typical use cases
Performance improvements:
- Time: 907.4 ns/op → 616.7 ns/op (32% faster)
- Memory: 6,120 B/op → 4,992 B/op (18% less)
- Allocations: 8 → 2 (75% reduction)
Impact:
- Significantly reduces allocation overhead
- Better performance for typical flatmap operations
- May over-allocate if mapper returns <2 items on average
Added BenchmarkFlatmap to track performance.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>