Skip to content

Commit 9fd2e44

Browse files
mknyszekgopherbot
authored andcommitted
runtime: add AddCleanup benchmark
Change-Id: Ia463a9b3b5980670bcf9297b4bddb60980ebfde5 Reviewed-on: https://go-review.googlesource.com/c/go/+/720320 Auto-Submit: Michael Knyszek <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 80c91ee commit 9fd2e44

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/runtime/mcleanup_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,31 @@ func TestCleanupLost(t *testing.T) {
336336
t.Errorf("expected %d cleanups to be executed, got %d", got, want)
337337
}
338338
}
339+
340+
// BenchmarkAddCleanupAndStop benchmarks adding and removing a cleanup
341+
// from the same allocation.
342+
//
343+
// At face value, this benchmark is unrealistic, since no program would
344+
// do this in practice. However, adding cleanups to new allocations in a
345+
// loop is also unrealistic. It adds additional unused allocations,
346+
// exercises uncommon performance pitfalls in AddCleanup (traversing the
347+
// specials list, which should just be its own benchmark), and executing
348+
// cleanups at a frequency that is unlikely to appear in real programs.
349+
//
350+
// This benchmark is still useful however, since we can get a low-noise
351+
// measurement of the cost of AddCleanup and Stop all in one without the
352+
// above pitfalls: we can measure the pure overhead. We can then separate
353+
// out the cost of each in CPU profiles if we so choose (they're not so
354+
// inexpensive as to make this infeasible).
355+
func BenchmarkAddCleanupAndStop(b *testing.B) {
356+
b.ReportAllocs()
357+
358+
type T struct {
359+
v int
360+
p unsafe.Pointer
361+
}
362+
x := new(T)
363+
for b.Loop() {
364+
runtime.AddCleanup(x, func(int) {}, 14).Stop()
365+
}
366+
}

0 commit comments

Comments
 (0)