parallel: remove mutex from Map and Times

The slice is pre-allocated, a mutex is not needed.
This commit is contained in:
francisco souza
2022-03-05 17:44:59 -05:00
parent 094b6f974f
commit 25fe06d08e

View File

@@ -7,7 +7,6 @@ import "sync"
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R { func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
result := make([]R, len(collection)) result := make([]R, len(collection))
var mu sync.Mutex
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(len(collection)) wg.Add(len(collection))
@@ -15,9 +14,7 @@ func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
go func(_item T, _i int) { go func(_item T, _i int) {
res := iteratee(_item, _i) res := iteratee(_item, _i)
mu.Lock()
result[_i] = res result[_i] = res
mu.Unlock()
wg.Done() wg.Done()
}(item, i) }(item, i)
@@ -50,7 +47,6 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
func Times[T any](count int, iteratee func(int) T) []T { func Times[T any](count int, iteratee func(int) T) []T {
result := make([]T, count) result := make([]T, count)
var mu sync.Mutex
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(count) wg.Add(count)
@@ -58,9 +54,7 @@ func Times[T any](count int, iteratee func(int) T) []T {
go func(_i int) { go func(_i int) {
item := iteratee(_i) item := iteratee(_i)
mu.Lock()
result[_i] = item result[_i] = item
mu.Unlock()
wg.Done() wg.Done()
}(i) }(i)