Skip to content

Commit

Permalink
runtime: move pinner variable into inner loop for benchmarks
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mknyszek authored and gopherbot committed May 23, 2023
1 parent 527d0e8 commit 94c7523
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/runtime/pinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand All @@ -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])
Expand All @@ -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])
}
Expand All @@ -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)
Expand All @@ -449,32 +446,29 @@ 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()
}
})
}

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()
}
})
}

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)
Expand Down

0 comments on commit 94c7523

Please sign in to comment.