1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:50:05 -07:00

errors: optimize *joinError's Error method for less allocation and faster execution

Handle the case of one error at the beginning.
Use unsafe.String to avoid memory allocation when converting byte slice to string.

Change-Id: Ib23576f72b1d87489e6f17762be483f62ca4998a
GitHub-Last-Rev: ed8003bfbc
GitHub-Pull-Request: golang/go#60026
Reviewed-on: https://go-review.googlesource.com/c/go/+/493237
Reviewed-by: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Jes Cok 2023-06-06 23:25:42 +00:00 committed by Damien Neil
parent 17d67ed0c9
commit 26fc4aa956

View File

@ -4,6 +4,10 @@
package errors
import (
"unsafe"
)
// Join returns an error that wraps the given errors.
// Any nil error values are discarded.
// Join returns nil if every value in errs is nil.
@ -38,14 +42,19 @@ type joinError struct {
}
func (e *joinError) Error() string {
var b []byte
for i, err := range e.errs {
if i > 0 {
b = append(b, '\n')
}
// Since Join returns nil if every value in errs is nil,
// e.errs cannot be empty.
if len(e.errs) == 1 {
return e.errs[0].Error()
}
b := []byte(e.errs[0].Error())
for _, err := range e.errs[1:] {
b = append(b, '\n')
b = append(b, err.Error()...)
}
return string(b)
// At this point, b has at least one byte '\n'.
return unsafe.String(&b[0], len(b))
}
func (e *joinError) Unwrap() []error {