Go 1.22 Release Notes(draft)
tip.golang.orgwhat is going to be the impact in terms of performance of allocating a new variable for each iteration of the loop ?
Is the compiler smart enough to only do this when it's needed ?
It will try to do this, but certainly unable to do this for all cases, such as
in which, the big array a will be duplicated at each step.func foo(constFunc func(*[100000]int)) { for a, i := [100000]int{}, 0; i < len(a); i++ { constFunc(&a) } }Performance downgrade is just one problem, the most serious problem is it will break some code, such as https://gotipplay.golang.org/p/srBxORL4bm__b. The code outputs differently with 1.22 and 1.22-.
The compiler will avoid unnecessary allocations when possible. For more details, see https://go.dev/wiki/LoopvarExperiment#will-the-change-make-p...