Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task pool #37

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add benchmark documentation
  • Loading branch information
Azer0s committed Mar 14, 2022
commit 121f17539f1970ffe0d0850432de87855b65b6bd
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,43 @@ lop.TaskPool(10, 4, func(i int) {
})
```

The TaskPool approach is quite a bit faster than the old parallel Map implementation, beating the old lop.Map implementation by a factor of about 4x.
The normal Map implementation is the fastest of them all in most situations.

```
goos: darwin
goarch: arm64
pkg: github.com/samber/lo/parallel
BenchmarkNormalMap
BenchmarkNormalMap-8 6790 174719 ns/op
BenchmarkNewTaskPool
BenchmarkNewTaskPool-8 272 4418947 ns/op
BenchmarkMap
BenchmarkMap-8 60 18902713 ns/op
PASS
```

The parallel implementations really starts to shine with long running callbacks. Adding a delay of just 1ms to the previous tests yields the following results:

```
goos: darwin
goarch: arm64
pkg: github.com/samber/lo/parallel
BenchmarkNormalMap
BenchmarkNormalMap-8 1 115098105417 ns/op
BenchmarkNewTaskPool
BenchmarkNewTaskPool-8 1 14409710791 ns/op
BenchmarkMap
BenchmarkMap-8 38 30249124 ns/op
PASS
```

So while the new TaskPool beats out the old lop.Map implementation in most cases,
the old lop.Map implementation beats the TaskPool when **all** jobs are long running.
The TaskPool delivers the best performance on average with some long running
and some quick jobs, beating out the normal Map implementation and the old lop.Map implementation.
In long running jobs the TaskPool beats the normal Map implementation by a factor of about 8x.

## 🛩 Benchmark

We executed a simple benchmark with the a dead-simple `lo.Map` loop:
Expand Down
26 changes: 22 additions & 4 deletions parallel/task_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,31 @@ func init() {
}
}

func normalMap[T any, R any](collection []T, iteratee func(T, int) R) []R {
result := make([]R, len(collection))

for i, item := range collection {
result[i] = iteratee(item, i)
}

return result
}

var callback = func(t, _ int) float64 {
return float64(t)
}

func BenchmarkNormalMap(b *testing.B) {
for n := 0; n < b.N; n++ {
normalMap[int, float64](c, callback)
}
}

func BenchmarkNewTaskPool(b *testing.B) {
result := make([]float64, l)

for n := 0; n < b.N; n++ {
TaskPool[int](len(c), runtime.NumCPU()/2, func(i int) {
TaskPool[int](len(c), runtime.NumCPU(), func(i int) {
result[i] = float64(c[i])
})
}
Expand Down Expand Up @@ -50,8 +70,6 @@ func oldMap[T any, R any](collection []T, iteratee func(T, int) R) []R {

func BenchmarkMap(b *testing.B) {
for n := 0; n < b.N; n++ {
oldMap[int, float64](c, func(t, _ int) float64 {
return float64(t)
})
oldMap[int, float64](c, callback)
}
}