aboutsummaryrefslogtreecommitdiff
path: root/src/errors
diff options
context:
space:
mode:
authorJes Cok <xigua67damn@gmail.com>2023-06-06 23:25:42 +0000
committerDamien Neil <dneil@google.com>2023-07-31 18:37:32 +0000
commit26fc4aa956783268d7a289240a028774e1ea3878 (patch)
treee49996219c9432400cd5642ebd3b8885a8f5b836 /src/errors
parent17d67ed0c904871bb2738ec7d1ed7831924c9aaf (diff)
downloadgo-26fc4aa956783268d7a289240a028774e1ea3878.tar.xz
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: ed8003bfbcae8efd42e54895db0554c139b9d3a7 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>
Diffstat (limited to 'src/errors')
-rw-r--r--src/errors/join.go21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/errors/join.go b/src/errors/join.go
index 1c486d591e..349fc06ed9 100644
--- a/src/errors/join.go
+++ b/src/errors/join.go
@@ -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 {