From 85b5d11246a2c768646f49f3c6750a139ecd1b21 Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Fri, 3 Jan 2025 19:06:07 +1100 Subject: [PATCH] crypto/subtle: add additional benchmarks for XORBytes Provide alignment benchmarks for XORBytes, as well as including 8192 byte blocks in the existing benchmarks. This allows us to better evaluate performance with unaligned inputs. Change-Id: Iad497c594c0425389ae02ca848aede5cb0ac3afd Reviewed-on: https://go-review.googlesource.com/c/go/+/639316 Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI Reviewed-by: Filippo Valsorda Reviewed-by: Roland Shoemaker --- src/crypto/subtle/xor_test.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/crypto/subtle/xor_test.go b/src/crypto/subtle/xor_test.go index 2e2169db0a..855e54d82b 100644 --- a/src/crypto/subtle/xor_test.go +++ b/src/crypto/subtle/xor_test.go @@ -92,7 +92,7 @@ func BenchmarkXORBytes(b *testing.B) { dst := make([]byte, 1<<15) data0 := make([]byte, 1<<15) data1 := make([]byte, 1<<15) - sizes := []int64{1 << 3, 1 << 7, 1 << 11, 1 << 15} + sizes := []int64{1 << 3, 1 << 7, 1 << 11, 1 << 13, 1 << 15} for _, size := range sizes { b.Run(fmt.Sprintf("%dBytes", size), func(b *testing.B) { s0 := data0[:size] @@ -105,6 +105,26 @@ func BenchmarkXORBytes(b *testing.B) { } } +func BenchmarkXORBytesAlignment(b *testing.B) { + dst := make([]byte, 8+1<<11) + data0 := make([]byte, 8+1<<11) + data1 := make([]byte, 8+1<<11) + sizes := []int64{1 << 3, 1 << 7, 1 << 11} + for _, size := range sizes { + for offset := int64(0); offset < 8; offset++ { + b.Run(fmt.Sprintf("%dBytes%dOffset", size, offset), func(b *testing.B) { + d := dst[offset : offset+size] + s0 := data0[offset : offset+size] + s1 := data1[offset : offset+size] + b.SetBytes(int64(size)) + for i := 0; i < b.N; i++ { + XORBytes(d, s0, s1) + } + }) + } + } +} + func mustPanic(t *testing.T, expected string, f func()) { t.Helper() defer func() { -- 2.51.0