diff options
| author | Junyang Shao <shaojunyang@google.com> | 2025-10-30 19:14:57 +0000 |
|---|---|---|
| committer | Junyang Shao <shaojunyang@google.com> | 2025-11-21 12:49:20 -0800 |
| commit | 22f24f90b52958158a2de5fa68adf0faf7699e4e (patch) | |
| tree | c71d09d9e3d69eb01d62f52df195f27c2edeb4a8 /src/testing | |
| parent | cfb9d2eb73d9ecb0e5ea2ce5ef0d3f23593087be (diff) | |
| download | go-22f24f90b52958158a2de5fa68adf0faf7699e4e.tar.xz | |
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/testing')
| -rw-r--r-- | src/testing/benchmark.go | 14 |
1 files changed, 8 insertions, 6 deletions
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. |
