This repository was archived by the owner on Dec 1, 2021. It is now read-only.
Move benchmark assigned err to global exported variable #106
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
toperris not used, but the go compiler itself doesn't detect thisbecause 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.
Example playground showing Go compiler is OK with unused variables in anonymous functions: https://play.golang.org/p/MEBjbLDWzC but go/types is not: https://play.golang.org/p/m7Gt4tEgS2.
benchstatcompare of master and this branch, but I couldn't find a quiet enough machine to better show no change./cc @kardianos this was originally added in 1d2e603 so you're probably best to review if you have time.