Files
lo/optimizations2.txt
T
Samuel Berthe dc372c082b perf: use direct index assignment in RepeatBy, Fill, Repeat (#834)
Replace make([]T, 0, count) + append with make([]T, count) + result[i] = ...
since the final size is known upfront. Eliminates per-element append overhead.

Also adds comprehensive benchmark coverage for all helpers across
core, it, mutable, and parallel packages (282 benchmark functions).
2026-03-06 00:04:16 +01:00

34 lines
1.9 KiB
Plaintext

# Performance Optimizations Report
## PR #827 - Reject / RejectErr / RejectMap
- Preallocate result slice with `make(Slice, 0, len(collection))` instead of `Slice{}`
- Avoids repeated grow-and-copy reallocations as elements are appended
## PR #829 - UniqMap
- Collect results directly during the single map-building pass instead of calling `Keys(seen)` afterward
- Eliminates a second full iteration over the map to extract keys
- Eliminates a separate slice allocation inside `Keys()`
- Preserves insertion order (old code returned non-deterministic map iteration order)
## PR #830 - Difference
- Preallocate result slices with `make(Slice, 0, len(listN))` instead of `Slice{}`
- Avoids repeated grow-and-copy reallocations as elements are appended
## PR #831 - FromSlicePtr / FromSlicePtrOr
- Replace indirect `Map()` call with closure by a direct loop
- Eliminates per-element function call overhead from the closure indirection
## PR #833 - CountValues, UniqKeys, UniqValues, FilterKeys, FilterValues, FilterKeysErr, FilterValuesErr
- Add size hint to map allocation in CountValues: `make(map[T]int)` → `make(map[T]int, len(collection))`
- Avoids repeated map rehashing as entries are added
- Add capacity hint to result slice in UniqKeys/UniqValues: `make([]K, 0)` → `make([]K, 0, size)`
- Avoids repeated slice grow-and-copy reallocations
- Add capacity hint to result slice in FilterKeys/FilterValues/FilterKeysErr/FilterValuesErr: `make([]K, 0)` → `make([]K, 0, len(in))`
- Avoids repeated slice grow-and-copy reallocations
## PR #??? - RepeatBy / Fill / Repeat
- Replace `make([]T, 0, count)` + `append(result, ...)` with `make([]T, count)` + `result[i] = ...`
- Eliminates per-element append overhead (bounds check, length increment, slice header update)
- Size is known upfront so direct index assignment is safe and faster
- RepeatByErr left unchanged: early error return makes direct index problematic (would allocate full slice then discard)