]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: move pinner variable into inner loop for benchmarks
authorMichael Anthony Knyszek <mknyszek@google.com>
Tue, 23 May 2023 19:17:51 +0000 (19:17 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 23 May 2023 21:35:21 +0000 (21:35 +0000)
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 <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
Auto-Submit: Michael Knyszek <mknyszek@google.com>

src/runtime/pinner_test.go

index 7c6dd1c21a3b4f4d215d5e09b525d557c9558594..1caebcb265967b5ba8928d6977afc86b15be2171 100644 (file)
@@ -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)