File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments