Experimental support of SIMD (go 1.27+)
A few helpers might have SIMD variants:
- [x] Sum
- [x] Mean
- [x] Min
- [x] Max
- [x] Clamp
- [x] SumBy
- [x] MeanBy
- [ ] Product
- [ ] Abs
- [ ] Sqrt
- [x] Contains
- [ ] Every
- [ ] Some
- [ ] None
- [ ] IndexOf
- [ ] HasPrefix / HasSuffix
- [ ] Find / FindIndexOf / FindLastIndexOf
- [ ] FindOrElse
- [ ] MinIndex / MaxIndex
- More ?
See #801, #1005
We won't recode arrow in lo, but I'm open to discussion for a high-level pipelining abstraction. Example:
- Pythagoras's theorem:
Sqrt(Mul(a, a), Mul(b, b)) - Average age of adults:
Mean(GreaterThan(18, age))
EDIT: #1005 brings support for Go 1.27 SIMD experimental lib. I've made some breaking changes but the API is getting simpler, thanks to the new cross-platform simd package of the stdlib. For the Go 1.26 implementation, please stick to github.com/samber/[email protected]
I tried adding simd support for just Sum and recorded the benchmark results.
I get the following:
** Performance without SIMD:**
goos: linux
goarch: amd64
pkg: github.com/samber/lo/benchmark
cpu: AMD Ryzen 9 8940HX with Radeon Graphics
BenchmarkSum/0-32 1000000000 0.4009 ns/op
BenchmarkSum/1-32 1000000000 0.5925 ns/op
BenchmarkSum/4-32 985769302 1.223 ns/op
BenchmarkSum/8-32 592880586 1.989 ns/op
BenchmarkSum/10-32 494444780 2.397 ns/op
BenchmarkSum/100-32 57876046 20.45 ns/op
BenchmarkSum/1000-32 6032308 201.2 ns/op
BenchmarkSum/10000-32 605767 1959 ns/op
BenchmarkSum/100000-32 58994 19750 ns/op
PASS
ok github.com/samber/lo/benchmark 10.471s
** Performance with SIMD:**
goos: linux
goarch: amd64
pkg: github.com/samber/lo/benchmark
cpu: AMD Ryzen 9 8940HX with Radeon Graphics
BenchmarkSum/0-32 488891706 2.455 ns/op
BenchmarkSum/1-32 366151856 3.309 ns/op
BenchmarkSum/4-32 338391091 3.497 ns/op
BenchmarkSum/8-32 337771704 3.504 ns/op
BenchmarkSum/10-32 300877652 3.986 ns/op
BenchmarkSum/100-32 100000000 10.31 ns/op
BenchmarkSum/1000-32 12397108 95.63 ns/op
BenchmarkSum/10000-32 1000000 1040 ns/op
BenchmarkSum/100000-32 118058 10282 ns/op
PASS
ok github.com/samber/lo/benchmark 12.403s
This obviously has the setup overhead. The idea of lo is simplifying operations and chaining them together as we do in FP. Pipelining might be a better option to work on.
** Pipelining approach: **
We can make use of iter.Seq
For example:
func Filter[V any](seq iter.Seq[V], keep func(V) bool) iter.Seq[V] {
return func(yield func(V) bool) {
for val := range seq {
if keep(val) {
if !yield(val) {
return
}
}
}
}
}
func Map[In, Out any](seq iter.Seq[In], transform func(In) Out) iter.Seq[Out] {
return func(yield func(Out) bool) {
for val := range seq {
if !yield(transform(val)) {
return
}
}
}
}
func Sum(seq iter.Seq[int]) int {
total := 0
for val := range seq {
total += val
}
return total
}
evens := Filter(seq, func(x int) bool { return x%2 == 0 })
doubled := Map(evens, func(x int) int { return x * 2 })
total := Sum(doubled)
This utilizes zero allocation. Sum becomes the final consumer of the data which is being generated by the Map and Filter when they're chained together
I made some experiment in https://github.com/samber/ro as well.
At some point, I think you should move to https://github.com/apache/arrow-go instead of doing weird stuff with samber/lo.
If you want to build a very simple pipeline API for lo, combining SIMD+iter, I would love to read your PR !
https://github.com/samber/lo/pull/1005 brings support for Go 1.27 SIMD experimental lib. I've made some breaking changes but the API is getting simpler, thanks to the new cross-platform simd package of the stdlib.
For the Go 1.26 implementation, please stick to github.com/samber/[email protected]