Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit bfd5150

Browse files
bradleyfalzondavecheney
authored andcommitted
Move benchmark assigned err to global exported variable (#106)
toperr is not used, but the go compiler itself doesn't detect this because it's within an anonymous function. However, go/types does detect this as being unused, which causes any static analysis tools which uses go/types' type checker to fail with the message "toperr assigned and not used". The final result of the benchmarked function is instead assigned to an exported global variable to ensure the compiler cannot now, nor in the future optimise away the function calls due to no observable side effects. It was chosen to assign the final result, after the benchmark loop, to the global variable, as this best follows the example set in the CL https://go-review.googlesource.com/#/c/37195/. As opposed to having each call to f assign to the global. This also appears to better align with the original author's intention of toperr. This change had no observable impact on the benchmark. Related golang/go#3059. Related golang/go#8560. Thanks dominikh for additional clarifications.
1 parent 248dadf commit bfd5150

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

bench_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ func yesErrors(at, depth int) error {
2323
return yesErrors(at+1, depth)
2424
}
2525

26+
// GlobalE is an exported global to store the result of benchmark results,
27+
// preventing the compiler from optimising the benchmark functions away.
28+
var GlobalE error
29+
2630
func BenchmarkErrors(b *testing.B) {
27-
var toperr error
2831
type run struct {
2932
stack int
3033
std bool
@@ -54,7 +57,7 @@ func BenchmarkErrors(b *testing.B) {
5457
err = f(0, r.stack)
5558
}
5659
b.StopTimer()
57-
toperr = err
60+
GlobalE = err
5861
})
5962
}
6063
}

0 commit comments

Comments
 (0)