Last active
November 28, 2015 21:05
-
-
Save scott-maddox/2e0e29a81c148e37d66b to your computer and use it in GitHub Desktop.
Inlining simple math stub functions fails
This file contains hidden or 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 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