Use append instead of copy to clone slices (#58)

The append notation is more concise and should be more efficient, since
the target slice won't be initialized with zero values first.
This commit is contained in:
Tom Wieczorek 2022-05-18 16:18:51 +02:00 committed by GitHub
parent 6fede5ccb3
commit f46d400df0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 6 deletions

View File

@ -209,10 +209,7 @@ func Errors(err error) []error {
return []error{err}
}
errors := eg.Errors()
result := make([]error, len(errors))
copy(result, errors)
return result
return append(([]error)(nil), eg.Errors()...)
}
// multiError is an error that holds one or more errors.
@ -393,8 +390,7 @@ func fromSlice(errors []error) error {
// Otherwise "errors" escapes to the heap
// unconditionally for all other cases.
// This lets us optimize for the "no errors" case.
out := make([]error, len(errors))
copy(out, errors)
out := append(([]error)(nil), errors...)
return &multiError{errors: out}
}
}