diff options
| author | j178 <sherlockjoe8@gmail.com> | 2023-08-18 12:04:35 +0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-08-18 23:40:44 +0000 |
| commit | ba7ba9bcc0e755f2c072d308e1c9f79bb2564e03 (patch) | |
| tree | 66ee955003e8e630c33c2325df5e2ed0edb370b7 /src/errors/wrap_test.go | |
| parent | 8789b5d72fe5a3f6c341d6f1b1f0097b5514657f (diff) | |
| download | go-ba7ba9bcc0e755f2c072d308e1c9f79bb2564e03.tar.xz | |
errors: optimize Is and As by reusing reflection of target
goos: darwin
goarch: amd64
pkg: errors
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
│ old │ new │
│ sec/op │ sec/op vs base │
Is-12 133.4n ± 0% 126.8n ± 3% -4.91% (p=0.001 n=10)
As-12 464.1n ± 1% 307.2n ± 0% -33.80% (p=0.000 n=10)
geomean 248.8n 197.4n -20.66%
│ old │ new │
│ B/op │ B/op vs base │
Is-12 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹
As-12 40.00 ± 0% 40.00 ± 0% ~ (p=1.000 n=10) ¹
geomean 30.98 30.98 +0.00%
¹ all samples are equal
│ old │ new │
│ allocs/op │ allocs/op vs base │
Is-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹
As-12 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹
geomean 1.414 1.414 +0.00%
¹ all samples are equal
Change-Id: I0844f3ab77e63b5f773594157dcffaffffd5e70d
Reviewed-on: https://go-review.googlesource.com/c/go/+/520756
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Diffstat (limited to 'src/errors/wrap_test.go')
| -rw-r--r-- | src/errors/wrap_test.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/errors/wrap_test.go b/src/errors/wrap_test.go index ca9dc0f111..0a7bc5d16a 100644 --- a/src/errors/wrap_test.go +++ b/src/errors/wrap_test.go @@ -238,6 +238,27 @@ func TestAsValidation(t *testing.T) { } } +func BenchmarkIs(b *testing.B) { + err1 := errors.New("1") + err2 := multiErr{multiErr{multiErr{err1, errorT{"a"}}, errorT{"b"}}} + + for i := 0; i < b.N; i++ { + if !errors.Is(err2, err1) { + b.Fatal("Is failed") + } + } +} + +func BenchmarkAs(b *testing.B) { + err := multiErr{multiErr{multiErr{errors.New("a"), errorT{"a"}}, errorT{"b"}}} + for i := 0; i < b.N; i++ { + var target errorT + if !errors.As(err, &target) { + b.Fatal("As failed") + } + } +} + func TestUnwrap(t *testing.T) { err1 := errors.New("1") erra := wrapped{"wrap 2", err1} |
