]> Cypherpunks repositories - gostls13.git/commitdiff
unicode/utf8: refactor benchmarks for FullRune function
authoreric fang <eric.fang@arm.com>
Wed, 13 May 2020 06:38:39 +0000 (06:38 +0000)
committerIan Lance Taylor <iant@golang.org>
Thu, 10 Sep 2020 20:25:45 +0000 (20:25 +0000)
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>
src/unicode/utf8/utf8_test.go

index 359461bd0579102f3a0902aecad2799ae772d9e5..eaf1b5ffee39e00a464c7ed00cf1b2267dc5ad62 100644 (file)
@@ -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)
+                       }
+               })
        }
 }