From 07d05425bbb57ceaff9ea2c31f135ff7434bb309 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 14 Nov 2025 14:32:05 +0100 Subject: [PATCH] perf: improve GroupBy map initialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes useless capacity hint of 0 for map initialization. Uses estimated capacity of len(values)/10 to reduce map resizing operations. Changes: - Before: make(map[K][]V, 0) - capacity 0 is meaningless for maps - After: make(map[K][]V, len(values)/10) - reasonable estimate Impact: - Reduces map resizing overhead during population - Assumes ~10% unique keys (reasonable for grouping operations) - May over-allocate for high cardinality, but acceptable trade-off This is a minor optimization that avoids repeated map growth when keys are added. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- groupby.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/groupby.go b/groupby.go index e4a554b..7b5db2d 100644 --- a/groupby.go +++ b/groupby.go @@ -2,7 +2,7 @@ package underscore // GroupBy splits a slice into a map[K][]V grouped by the result of the iterator function. func GroupBy[K comparable, V any](values []V, f func(V) K) map[K][]V { - res := make(map[K][]V, 0) + res := make(map[K][]V, len(values)/10) for _, v := range values { k := f(v) if r, ok := res[k]; ok {