Skip to content

Commit ab29262

Browse files
thepuddsgopherbot
authored andcommitted
testing: clarify how B.Loop avoids optimizing away all the useful work
As discussed in #73137, we want to clarify the description of how B.Loop avoids surprising optimizations, while also hinting that the exact approach might change in the future. Updates #73137 Change-Id: I8536540cd5d79804a47fba8cd6eec3821864309d Reviewed-on: https://go-review.googlesource.com/c/go/+/662356 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent ce94e91 commit ab29262

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Diff for: src/testing/benchmark.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,13 @@ func (b *B) loopSlowPath() bool {
477477
// toward the benchmark measurement. Likewise, when it returns false, it stops
478478
// the timer so cleanup code is not measured.
479479
//
480-
// The compiler never optimizes away calls to functions within the body of a
481-
// "for b.Loop() { ... }" loop. This prevents surprises that can otherwise occur
482-
// if the compiler determines that the result of a benchmarked function is
483-
// unused. The loop condition must be written in exactly as "b.Loop()", and this
484-
// only applies to calls syntactically between the curly braces of the loop.
485-
// Optimizations are performed as usual in any functions called by the loop.
480+
// Within the body of a "for b.Loop() { ... }" loop, arguments to and
481+
// results from function calls within the loop are kept alive, preventing
482+
// the compiler from fully optimizing away the loop body. Currently, this is
483+
// implemented by disabling inlining of functions called in a b.Loop loop.
484+
// This applies only to calls syntactically between the curly braces of the loop,
485+
// and the loop condition must be written exactly as "b.Loop()". Optimizations
486+
// are performed as usual in any functions called by the loop.
486487
//
487488
// After Loop returns false, b.N contains the total number of iterations that
488489
// ran, so the benchmark may use b.N to compute other average metrics.

0 commit comments

Comments
 (0)