perf(array): pre-size Ap and Flatten results#190
Merged
Conversation
array.Ap and array.Flatten both routed through MonadChain, which grows the result from nil and (for Ap) allocates an intermediate slice per function. Their output lengths are known up front -- len(fab)*len(fa) for the applicative cartesian product and the sum of inner lengths for flatten -- so build the result in a single pre-sized allocation. Benchmarks (added): BenchmarkAp 6 -> 1 allocs/op (-83%), 155 -> 66 ns/op BenchmarkFlatten 3 -> 1 allocs/op (-67%), 51 -> 24 ns/op Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>
CarstenLeue
requested changes
Jun 26, 2026
| // each a in fa, so its length is known up front. Pre-sizing avoids the | ||
| // per-function intermediate slice (one MonadMap allocation each) and the | ||
| // append-growth reallocations of the previous MonadChain-based formulation. | ||
| result := make(BS, 0, len(fab)*len(fa)) |
Collaborator
There was a problem hiding this comment.
in the same spirit as the slices.Concat function we can use slices.Grow to have a common way of allocating the slice, and accept nil for the result of an empty result.
Address review feedback by replacing the hand-rolled pre-sizing with the idiomatic standard library helpers (Go 1.21+, compatible with the module's 1.24 requirement): - Flatten delegates to slices.Concat, which sizes the result in a single allocation. - MonadAp allocates its result once via slices.Grow, the same idiom slices.Concat uses internally. Both return a nil slice for an empty result, which is a valid representation of the empty array. The doc comments now state this, and the TestNilSlice_* assertions are relaxed to check only that the result is empty. Benchmarks are unchanged at a single allocation per call: BenchmarkAp 240 B/op, 1 allocs/op BenchmarkFlatten 128 B/op, 1 allocs/op Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>
Carsten-Leue
approved these changes
Jun 28, 2026
CarstenLeue
approved these changes
Jun 28, 2026
|
🎉 This PR is included in version 2.3.69 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
array.Apandarray.Flattenboth route throughMonadChain, which grows theresult from
nil;Apadditionally allocates an intermediate slice per function(via
MonadMap).Change
Both output lengths are known up front —
len(fab)*len(fa)for the applicativecartesian product, and the sum of the inner slice lengths for flatten — so the
result is built in a single pre-sized allocation. Element order and values are
unchanged (the change is in
array/generic, which the publicarray.Ap/array.Flattendelegate to).Result
Benchmarks (added),
-benchmem -count=6:ApbeforeApafterFlattenbeforeFlattenafter−83% / −67% allocations respectively.
Testing
go test -race ./array/...passes (existing tests cover element order/values).BenchmarkApandBenchmarkFlattenwere added to demonstrate and guard the win.