• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IBM / fp-go / 28335138290

28 Jun 2026 08:34PM UTC coverage: 62.823% (-0.006%) from 62.829%
28335138290

push

github

web-flow
perf(array): pre-size Ap and Flatten results (#190)

* perf(array): pre-size Ap and Flatten results

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>

* perf(array): use slices.Concat and slices.Grow for Flatten and Ap

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>

---------

Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>

33038 of 52589 relevant lines covered (62.82%)

533.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.16
/array/generic/array.go


Source Not Available

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc