From ba7ba9bcc0e755f2c072d308e1c9f79bb2564e03 Mon Sep 17 00:00:00 2001 From: j178 Date: Fri, 18 Aug 2023 12:04:35 +0800 Subject: errors: optimize Is and As by reusing reflection of target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Run-TryBot: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov --- src/errors/wrap_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/errors/wrap_test.go') 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} -- cgit v1.3