diff options
author | eric fang <eric.fang@arm.com> | 2020-05-13 06:38:39 +0000 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2020-09-10 20:25:45 +0000 |
commit | a1762c2cc67822d86cb37747a56f0d4a07d24ced (patch) | |
tree | 650d0e2e6974d1aa336341f076b38979983418f0 | |
parent | d7ab277eed4d2e5ede4f3361adf42d4ad76ced8f (diff) | |
download | go-a1762c2cc67822d86cb37747a56f0d4a07d24ced.tar.gz go-a1762c2cc67822d86cb37747a56f0d4a07d24ced.zip |
unicode/utf8: refactor benchmarks for FullRune function
BenchmarkFullASCIIRune tests the performance of function utf8.FullRune,
which will be inlined in BenchmarkFullASCIIRune. Since the return value
of FullRune is not referenced, it will be removed as dead code.
This CL makes the FullRune functions return value referenced by a global
variable to avoid this point. In addition, this CL adds one more benchmark
to cover more code paths, and puts them together as sub benchmarks of
BenchmarkFullRune.
Change-Id: I6e79f4c087adf70e351498a4b58d7482dcd1ec4a
Reviewed-on: https://go-review.googlesource.com/c/go/+/233979
Run-TryBot: eric fang <eric.fang@arm.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
-rw-r--r-- | src/unicode/utf8/utf8_test.go | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/unicode/utf8/utf8_test.go b/src/unicode/utf8/utf8_test.go index 359461bd05..eaf1b5ffee 100644 --- a/src/unicode/utf8/utf8_test.go +++ b/src/unicode/utf8/utf8_test.go @@ -597,16 +597,24 @@ func BenchmarkDecodeJapaneseRune(b *testing.B) { } } -func BenchmarkFullASCIIRune(b *testing.B) { - a := []byte{'a'} - for i := 0; i < b.N; i++ { - FullRune(a) - } -} - -func BenchmarkFullJapaneseRune(b *testing.B) { - nihon := []byte("本") - for i := 0; i < b.N; i++ { - FullRune(nihon) +// boolSink is used to reference the return value of benchmarked +// functions to avoid dead code elimination. +var boolSink bool + +func BenchmarkFullRune(b *testing.B) { + benchmarks := []struct { + name string + data []byte + }{ + {"ASCII", []byte("a")}, + {"Incomplete", []byte("\xf0\x90\x80")}, + {"Japanese", []byte("本")}, + } + for _, bm := range benchmarks { + b.Run(bm.name, func(b *testing.B) { + for i := 0; i < b.N; i++ { + boolSink = FullRune(bm.data) + } + }) } } |