perf: pre-allocate Partition result slices

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 <noreply@anthropic.com>
This commit is contained in:
Ruidy 2025-11-14 13:48:28 +01:00
parent 7caa23e082
commit 46d52e3cfa
No known key found for this signature in database
GPG key ID: 705C24D202990805

View file

@ -3,8 +3,8 @@ package underscore
// Partition splits the slice into two slices: one whose elements all satisfy predicate // Partition splits the slice into two slices: one whose elements all satisfy predicate
// and one whose elements all do not satisfy predicate. // and one whose elements all do not satisfy predicate.
func Partition[T any](values []T, predicate func(T) bool) ([]T, []T) { func Partition[T any](values []T, predicate func(T) bool) ([]T, []T) {
keep := make([]T, 0) keep := make([]T, 0, len(values))
reject := make([]T, 0) reject := make([]T, 0, len(values))
for _, v := range values { for _, v := range values {
if predicate(v) { if predicate(v) {