Skip to content

Commit

Permalink
Use append instead of copy to clone slices (#58)
Browse files Browse the repository at this point in the history
The append notation is more concise and should be more efficient, since
the target slice won't be initialized with zero values first.
  • Loading branch information
twz123 authored May 18, 2022
1 parent 6fede5c commit f46d400
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
}
}
Expand Down

0 comments on commit f46d400

Please sign in to comment.