I have a question about Go optimization.
In Go, even if a variable is declared within a loop, if it does not escape to the heap, the same stack address is reused across iterations instead of allocating a new memory address each time. After escape analysis determines whether a variable escapes, where in cmd/compile is this optimization performed?
Could you please help me with this question?
package main
func main() {
for i := 0; i < 3; i++ {
var x int
println(&x) // output same address
}
}
New contributor
user25395156 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.