Created
October 11, 2014 06:44
-
-
Save ejona86/a2397eecf47147927212 to your computer and use it in GitHub Desktop.
Suboptimal golang escape analysis test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Package escape allows you to observe suboptimal compiler escape analysis as | |
// seen in go1.3.3. Try with: `go build -gcflags -m escape.go' | |
package escape | |
// Compiler figures out that num does not escape. | |
func NoEscape() { | |
for i := 0; i < 2; i++ { | |
num := new(int) | |
_ = num | |
} | |
} | |
// Compiler figures out that num2 does not escape. | |
func NoEscape2() { | |
var num int | |
var num2 *int | |
for i := 0; i < 2; i++ { | |
num2 = &num | |
_ = num2 | |
} | |
} | |
// However, here the compiler can't figure out that num doesn't escsape. | |
func ShouldntEscape() { | |
var num *int | |
for i := 0; i < 2; i++ { | |
num = new(int) | |
_ = num | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment