Skip to content

Instantly share code, notes, and snippets.

@ejona86
Created October 11, 2014 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ejona86/a2397eecf47147927212 to your computer and use it in GitHub Desktop.
Save ejona86/a2397eecf47147927212 to your computer and use it in GitHub Desktop.
Suboptimal golang escape analysis test
// 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