1
0
mirror of https://github.com/golang/go synced 2024-11-27 01:11:20 -07:00

runtime: add 2-byte and 8-byte sub-benchmarks for memmove load/store

Change-Id: I6389d7efe90836b6ece44d2e75053d1ad9f35d08
Reviewed-on: https://go-review.googlesource.com/c/go/+/253417
Trust: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Heisenberg 2020-09-08 14:31:39 +08:00 committed by Keith Randall
parent f0c9ae5452
commit c515852732

View File

@ -538,21 +538,30 @@ func BenchmarkCopyFat1024(b *testing.B) {
}
}
// BenchmarkIssue18740 ensures that memmove uses 4 and 8 byte load/store to move 4 and 8 bytes.
// It used to do 2 2-byte load/stores, which leads to a pipeline stall
// when we try to read the result with one 4-byte load.
func BenchmarkIssue18740(b *testing.B) {
// This tests that memmove uses one 4-byte load/store to move 4 bytes.
// It used to do 2 2-byte load/stores, which leads to a pipeline stall
// when we try to read the result with one 4-byte load.
var buf [4]byte
for j := 0; j < b.N; j++ {
s := uint32(0)
for i := 0; i < 4096; i += 4 {
copy(buf[:], g[i:])
s += binary.LittleEndian.Uint32(buf[:])
}
sink = uint64(s)
benchmarks := []struct {
name string
nbyte int
f func([]byte) uint64
}{
{"2byte", 2, func(buf []byte) uint64 { return uint64(binary.LittleEndian.Uint16(buf)) }},
{"4byte", 4, func(buf []byte) uint64 { return uint64(binary.LittleEndian.Uint32(buf)) }},
{"8byte", 8, func(buf []byte) uint64 { return binary.LittleEndian.Uint64(buf) }},
}
var g [4096]byte
for _, bm := range benchmarks {
buf := make([]byte, bm.nbyte)
b.Run(bm.name, func(b *testing.B) {
for j := 0; j < b.N; j++ {
for i := 0; i < 4096; i += bm.nbyte {
copy(buf[:], g[i:])
sink += bm.f(buf[:])
}
}
})
}
}
// TODO: 2 byte and 8 byte benchmarks also.
var g [4096]byte