From 22f24f90b52958158a2de5fa68adf0faf7699e4e Mon Sep 17 00:00:00 2001 From: Junyang Shao Date: Thu, 30 Oct 2025 19:14:57 +0000 Subject: cmd/compile: change testing.B.Loop keep alive semantic This CL implements this initial design of testing.B.Loop's keep variable alive semantic: https://github.com/golang/go/issues/61515#issuecomment-2407963248. Fixes #73137. Change-Id: I8060470dbcb0dda0819334f3615cc391ff0f6501 Reviewed-on: https://go-review.googlesource.com/c/go/+/716660 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase --- src/testing/benchmark.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/testing') diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go index 437c2ec741..fbd82beb84 100644 --- a/src/testing/benchmark.go +++ b/src/testing/benchmark.go @@ -483,12 +483,14 @@ func (b *B) loopSlowPath() bool { // the timer so cleanup code is not measured. // // Within the body of a "for b.Loop() { ... }" loop, arguments to and -// results from function calls within the loop are kept alive, preventing -// the compiler from fully optimizing away the loop body. Currently, this is -// implemented by disabling inlining of functions called in a b.Loop loop. -// This applies only to calls syntactically between the curly braces of the loop, -// and the loop condition must be written exactly as "b.Loop()". Optimizations -// are performed as usual in any functions called by the loop. +// results from function calls and assignment receivers within the loop are kept +// alive, preventing the compiler from fully optimizing away the loop body. +// Currently, this is implemented as a compiler transformation that wraps such +// variables with a runtime.KeepAlive intrinsic call. The compiler can recursively +// walk the body of for, if statments, the cases of switch, select statments +// and bracket-braced blocks. This applies only to statements syntactically between +// the curly braces of the loop, and the loop condition must be written exactly +// as "b.Loop()". // // After Loop returns false, b.N contains the total number of iterations that // ran, so the benchmark may use b.N to compute other average metrics. -- cgit v1.3