From 46d52e3cfa179efa4b9b3a55c86aff352badb96d Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 14 Nov 2025 13:48:28 +0100 Subject: [PATCH] perf: pre-allocate Partition result slices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds capacity hints to both keep and reject slices in Partition function to prevent repeated allocations during append operations. Changes: - keep: make([]T, 0) → make([]T, 0, len(values)) - reject: make([]T, 0) → make([]T, 0, len(values)) Impact: - Reduces allocations from O(log n) to O(1) for each slice - Improves performance by eliminating slice growth overhead - Minimal memory overhead as worst case is original slice size 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- partition.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/partition.go b/partition.go index ebc038e..22fbe75 100644 --- a/partition.go +++ b/partition.go @@ -3,8 +3,8 @@ package underscore // Partition splits the slice into two slices: one whose elements all satisfy predicate // and one whose elements all do not satisfy predicate. func Partition[T any](values []T, predicate func(T) bool) ([]T, []T) { - keep := make([]T, 0) - reject := make([]T, 0) + keep := make([]T, 0, len(values)) + reject := make([]T, 0, len(values)) for _, v := range values { if predicate(v) {