mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
errors: optimize errors.Join for single unwrappable errors
Change-Id: I10bbb782ca7234cda8c82353f2255eec5be588c9 GitHub-Last-Rev: e5ad8fdb802e56bb36c41b4982ed27c1e0809af8 GitHub-Pull-Request: golang/go#70770 Reviewed-on: https://go-review.googlesource.com/c/go/+/635115 Auto-Submit: Sean Liao <sean@liao.dev> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Sean Liao <sean@liao.dev> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
0c0d257241
commit
80bff42fdd
@ -26,6 +26,18 @@ func Join(errs ...error) error {
|
|||||||
if n == 0 {
|
if n == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if n == 1 {
|
||||||
|
for _, err := range errs {
|
||||||
|
if err != nil {
|
||||||
|
if _, ok := err.(interface {
|
||||||
|
Unwrap() []error
|
||||||
|
}); ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
e := &joinError{
|
e := &joinError{
|
||||||
errs: make([]error, 0, n),
|
errs: make([]error, 0, n),
|
||||||
}
|
}
|
||||||
|
@ -70,3 +70,37 @@ func TestJoinErrorMethod(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkJoin(b *testing.B) {
|
||||||
|
for _, bb := range []struct {
|
||||||
|
name string
|
||||||
|
errs []error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "no error",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "single non-nil error",
|
||||||
|
errs: []error{errors.New("err")},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple errors",
|
||||||
|
errs: []error{errors.New("err"), errors.New("newerr"), errors.New("newerr2")},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unwrappable single error",
|
||||||
|
errs: []error{errors.Join(errors.New("err"))},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nil first error",
|
||||||
|
errs: []error{nil, errors.New("newerr")},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
b.Run(bb.name, func(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_ = errors.Join(bb.errs...)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user