From 4afefce20f9b00e927315b4c3cdc792fb824780c Mon Sep 17 00:00:00 2001 From: Camden Cheek Date: Mon, 8 Jan 2024 11:24:09 -0700 Subject: [PATCH] Multierror: join errors at the end (#132) This fixes `MapErr` so that it does not create recursive `joinError`s. By repeatedly calling `errors.Join(`, we create a nested set of `joinError`s rather than a single, flat `joinError`. Then, when `Error()` is called, it allocates a new string for each nested error because `joinError` calls `Error()` recursively on each of its children. Instead, this PR updates `MapErr` to just collect a slice of errors and return the flat joined error. --- iter/map.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iter/map.go b/iter/map.go index b311287..c5462c0 100644 --- a/iter/map.go +++ b/iter/map.go @@ -49,16 +49,16 @@ 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 + 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() - errs = multierror.Join(errs, err) + errs = append(errs, err) errMux.Unlock() } }) - return res, errs + return res, multierror.Join(errs...) }