Skip to content

Instantly share code, notes, and snippets.

@scott-maddox
Last active November 28, 2015 21:05
Show Gist options
  • Save scott-maddox/2e0e29a81c148e37d66b to your computer and use it in GitHub Desktop.
Save scott-maddox/2e0e29a81c148e37d66b to your computer and use it in GitHub Desktop.
Inlining simple math stub functions fails
package main
import (
"fmt"
"math"
)
func Sqr2() float64 {
return 2.0*2.0
}
func Sqrt2() float64 {
return math.Sqrt(2.0)
}
func main() {
fmt.Println(Sqr2)
fmt.Println(Sqrt2)
}
// $ go build -gcflags -m math.go
// # command-line-arguments
// ./math.go:8: can inline Sqr2
// ./math.go:17: Sqr2 escapes to heap
// ./math.go:18: Sqrt2 escapes to heap
// ./math.go:17: main ... argument does not escape
// ./math.go:18: main ... argument does not escape
// Note that Sqr2 can be inlined, but Sqrt2 cannot.
// While not important for this trivial example,
// it can result in signiciant and unnecessary overhead
// for simple custom types, e.g. vectors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment