From: Michael Anthony Knyszek Date: Tue, 23 May 2023 19:17:51 +0000 (+0000) Subject: runtime: move pinner variable into inner loop for benchmarks X-Git-Tag: go1.21rc1~315 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=94c75232fbe4e24c38f728fd05e0a151893fb2c5;p=gostls13.git runtime: move pinner variable into inner loop for benchmarks Currently the pinner is created outside of the benchmarking loop. However, this means that we get to reuse the same pinner for each loop; in general, users are expected to create a pinner for a e.g. a cgo call and then that variable will expire with the frame it lives in. (If they can reuse the variable, great! However, I don't expect that to be common.) In essence, this benchmarks a harder case. It's not more right or wrong than the previous version, but the fact that it's a slightly harder case (that still mostly captures what the original version was capturing) is useful. Change-Id: I94987127f54d7bfecd7b8e6a5e632631ea57ad24 Reviewed-on: https://go-review.googlesource.com/c/go/+/497616 Run-TryBot: Michael Knyszek TryBot-Result: Gopher Robot Reviewed-by: Austin Clements Auto-Submit: Michael Knyszek --- diff --git a/src/runtime/pinner_test.go b/src/runtime/pinner_test.go index 7c6dd1c21a..1caebcb265 100644 --- a/src/runtime/pinner_test.go +++ b/src/runtime/pinner_test.go @@ -376,9 +376,9 @@ func BenchmarkPinnerPinUnpinBatch(b *testing.B) { for i := 0; i < Batch; i++ { data[i] = new(obj) } - var pinner runtime.Pinner b.ResetTimer() for n := 0; n < b.N; n++ { + var pinner runtime.Pinner for i := 0; i < Batch; i++ { pinner.Pin(data[i]) } @@ -392,9 +392,9 @@ func BenchmarkPinnerPinUnpinBatchDouble(b *testing.B) { for i := 0; i < Batch; i++ { data[i] = new(obj) } - var pinner runtime.Pinner b.ResetTimer() for n := 0; n < b.N; n++ { + var pinner runtime.Pinner for i := 0; i < Batch; i++ { pinner.Pin(data[i]) pinner.Pin(data[i]) @@ -409,9 +409,9 @@ func BenchmarkPinnerPinUnpinBatchTiny(b *testing.B) { for i := 0; i < Batch; i++ { data[i] = new(bool) } - var pinner runtime.Pinner b.ResetTimer() for n := 0; n < b.N; n++ { + var pinner runtime.Pinner for i := 0; i < Batch; i++ { pinner.Pin(data[i]) } @@ -420,27 +420,24 @@ func BenchmarkPinnerPinUnpinBatchTiny(b *testing.B) { } func BenchmarkPinnerPinUnpin(b *testing.B) { - var pinner runtime.Pinner - b.ResetTimer() for n := 0; n < b.N; n++ { + var pinner runtime.Pinner pinner.Pin(new(obj)) pinner.Unpin() } } func BenchmarkPinnerPinUnpinTiny(b *testing.B) { - var pinner runtime.Pinner - b.ResetTimer() for n := 0; n < b.N; n++ { + var pinner runtime.Pinner pinner.Pin(new(bool)) pinner.Unpin() } } func BenchmarkPinnerPinUnpinDouble(b *testing.B) { - var pinner runtime.Pinner - b.ResetTimer() for n := 0; n < b.N; n++ { + var pinner runtime.Pinner p := new(obj) pinner.Pin(p) pinner.Pin(p) @@ -449,10 +446,9 @@ func BenchmarkPinnerPinUnpinDouble(b *testing.B) { } func BenchmarkPinnerPinUnpinParallel(b *testing.B) { - b.ResetTimer() b.RunParallel(func(pb *testing.PB) { - var pinner runtime.Pinner for pb.Next() { + var pinner runtime.Pinner pinner.Pin(new(obj)) pinner.Unpin() } @@ -460,10 +456,9 @@ func BenchmarkPinnerPinUnpinParallel(b *testing.B) { } func BenchmarkPinnerPinUnpinParallelTiny(b *testing.B) { - b.ResetTimer() b.RunParallel(func(pb *testing.PB) { - var pinner runtime.Pinner for pb.Next() { + var pinner runtime.Pinner pinner.Pin(new(bool)) pinner.Unpin() } @@ -471,10 +466,9 @@ func BenchmarkPinnerPinUnpinParallelTiny(b *testing.B) { } func BenchmarkPinnerPinUnpinParallelDouble(b *testing.B) { - b.ResetTimer() b.RunParallel(func(pb *testing.PB) { - var pinner runtime.Pinner for pb.Next() { + var pinner runtime.Pinner p := new(obj) pinner.Pin(p) pinner.Pin(p)