summaryrefslogtreecommitdiff
path: root/vendor/github.com/sourcegraph/conc/iter
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/sourcegraph/conc/iter')
-rw-r--r--vendor/github.com/sourcegraph/conc/iter/iter.go85
-rw-r--r--vendor/github.com/sourcegraph/conc/iter/map.go65
2 files changed, 150 insertions, 0 deletions
diff --git a/vendor/github.com/sourcegraph/conc/iter/iter.go b/vendor/github.com/sourcegraph/conc/iter/iter.go
new file mode 100644
index 000000000..124b4f940
--- /dev/null
+++ b/vendor/github.com/sourcegraph/conc/iter/iter.go
@@ -0,0 +1,85 @@
+package iter
+
+import (
+ "runtime"
+ "sync/atomic"
+
+ "github.com/sourcegraph/conc"
+)
+
+// defaultMaxGoroutines returns the default maximum number of
+// goroutines to use within this package.
+func defaultMaxGoroutines() int { return runtime.GOMAXPROCS(0) }
+
+// Iterator can be used to configure the behaviour of ForEach
+// and ForEachIdx. The zero value is safe to use with reasonable
+// defaults.
+//
+// Iterator is also safe for reuse and concurrent use.
+type Iterator[T any] struct {
+ // MaxGoroutines controls the maximum number of goroutines
+ // to use on this Iterator's methods.
+ //
+ // If unset, MaxGoroutines defaults to runtime.GOMAXPROCS(0).
+ MaxGoroutines int
+}
+
+// ForEach executes f in parallel over each element in input.
+//
+// It is safe to mutate the input parameter, which makes it
+// possible to map in place.
+//
+// ForEach always uses at most runtime.GOMAXPROCS goroutines.
+// It takes roughly 2µs to start up the goroutines and adds
+// an overhead of roughly 50ns per element of input. For
+// a configurable goroutine limit, use a custom Iterator.
+func ForEach[T any](input []T, f func(*T)) { Iterator[T]{}.ForEach(input, f) }
+
+// ForEach executes f in parallel over each element in input,
+// using up to the Iterator's configured maximum number of
+// goroutines.
+//
+// It is safe to mutate the input parameter, which makes it
+// possible to map in place.
+//
+// It takes roughly 2µs to start up the goroutines and adds
+// an overhead of roughly 50ns per element of input.
+func (iter Iterator[T]) ForEach(input []T, f func(*T)) {
+ iter.ForEachIdx(input, func(_ int, t *T) {
+ f(t)
+ })
+}
+
+// ForEachIdx is the same as ForEach except it also provides the
+// index of the element to the callback.
+func ForEachIdx[T any](input []T, f func(int, *T)) { Iterator[T]{}.ForEachIdx(input, f) }
+
+// ForEachIdx is the same as ForEach except it also provides the
+// index of the element to the callback.
+func (iter Iterator[T]) ForEachIdx(input []T, f func(int, *T)) {
+ if iter.MaxGoroutines == 0 {
+ // iter is a value receiver and is hence safe to mutate
+ iter.MaxGoroutines = defaultMaxGoroutines()
+ }
+
+ numInput := len(input)
+ if iter.MaxGoroutines > numInput {
+ // No more concurrent tasks than the number of input items.
+ iter.MaxGoroutines = numInput
+ }
+
+ var idx atomic.Int64
+ // Create the task outside the loop to avoid extra closure allocations.
+ task := func() {
+ i := int(idx.Add(1) - 1)
+ for ; i < numInput; i = int(idx.Add(1) - 1) {
+ f(i, &input[i])
+ }
+ }
+
+ var wg conc.WaitGroup
+ for i := 0; i < iter.MaxGoroutines; i++ {
+ wg.Go(task)
+ }
+ wg.Wait()
+}
diff --git a/vendor/github.com/sourcegraph/conc/iter/map.go b/vendor/github.com/sourcegraph/conc/iter/map.go
new file mode 100644
index 000000000..efbe6bfaf
--- /dev/null
+++ b/vendor/github.com/sourcegraph/conc/iter/map.go
@@ -0,0 +1,65 @@
+package iter
+
+import (
+ "sync"
+
+ "github.com/sourcegraph/conc/internal/multierror"
+)
+
+// Mapper is an Iterator with a result type R. It can be used to configure
+// the behaviour of Map and MapErr. The zero value is safe to use with
+// reasonable defaults.
+//
+// Mapper is also safe for reuse and concurrent use.
+type Mapper[T, R any] Iterator[T]
+
+// Map applies f to each element of input, returning the mapped result.
+//
+// Map always uses at most runtime.GOMAXPROCS goroutines. For a configurable
+// goroutine limit, use a custom Mapper.
+func Map[T, R any](input []T, f func(*T) R) []R {
+ return Mapper[T, R]{}.Map(input, f)
+}
+
+// Map applies f to each element of input, returning the mapped result.
+//
+// Map uses up to the configured Mapper's maximum number of goroutines.
+func (m Mapper[T, R]) Map(input []T, f func(*T) R) []R {
+ res := make([]R, len(input))
+ Iterator[T](m).ForEachIdx(input, func(i int, t *T) {
+ res[i] = f(t)
+ })
+ return res
+}
+
+// MapErr applies f to each element of the input, returning the mapped result
+// and a combined error of all returned errors.
+//
+// Map always uses at most runtime.GOMAXPROCS goroutines. For a configurable
+// goroutine limit, use a custom Mapper.
+func MapErr[T, R any](input []T, f func(*T) (R, error)) ([]R, error) {
+ return Mapper[T, R]{}.MapErr(input, f)
+}
+
+// MapErr applies f to each element of the input, returning the mapped result
+// and a combined error of all returned errors.
+//
+// Map uses up to the configured Mapper's maximum number of goroutines.
+func (m Mapper[T, R]) MapErr(input []T, f func(*T) (R, error)) ([]R, error) {
+ var (
+ res = make([]R, len(input))
+ errMux sync.Mutex
+ errs error
+ )
+ Iterator[T](m).ForEachIdx(input, func(i int, t *T) {
+ var err error
+ res[i], err = f(t)
+ if err != nil {
+ errMux.Lock()
+ // TODO: use stdlib errors once multierrors land in go 1.20
+ errs = multierror.Join(errs, err)
+ errMux.Unlock()
+ }
+ })
+ return res, errs
+}