Skip to content

Commit a1762c2

Browse files
eric fangianlancetaylor
authored andcommitted
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent d7ab277 commit a1762c2

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/unicode/utf8/utf8_test.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -597,16 +597,24 @@ func BenchmarkDecodeJapaneseRune(b *testing.B) {
597597
}
598598
}
599599

600-
func BenchmarkFullASCIIRune(b *testing.B) {
601-
a := []byte{'a'}
602-
for i := 0; i < b.N; i++ {
603-
FullRune(a)
604-
}
605-
}
606-
607-
func BenchmarkFullJapaneseRune(b *testing.B) {
608-
nihon := []byte("本")
609-
for i := 0; i < b.N; i++ {
610-
FullRune(nihon)
600+
// boolSink is used to reference the return value of benchmarked
601+
// functions to avoid dead code elimination.
602+
var boolSink bool
603+
604+
func BenchmarkFullRune(b *testing.B) {
605+
benchmarks := []struct {
606+
name string
607+
data []byte
608+
}{
609+
{"ASCII", []byte("a")},
610+
{"Incomplete", []byte("\xf0\x90\x80")},
611+
{"Japanese", []byte("本")},
612+
}
613+
for _, bm := range benchmarks {
614+
b.Run(bm.name, func(b *testing.B) {
615+
for i := 0; i < b.N; i++ {
616+
boolSink = FullRune(bm.data)
617+
}
618+
})
611619
}
612620
}

0 commit comments

Comments
 (0)