I'm learning about Go memory optimization through inlining. The following code is my test code.
I use go build -gcflags=-m=2 main.go
to get all the results. I follow Dave Cheney's post to do this.
My question is: What're the values of 'gcflags'? Where is the documentation, I couldn't find the doc by Google?
func fn1() int {
s := 1+2
return s
}
func fn2() string {
a := "h"
a += "e"
a += "l"
a += "l"
a += "o"
b := " "
c := "w"
c += "o"
c += "r"
c += "l"
c += "d"
d := "d"
d += "e"
d += "e"
d += "e"
d += "e"
d += "e"
d += "e"
d += "e"
d += "e"
d += "e"
s := a + b + c
return s
}
func fn3() {
p := fn1()
s := fn2()
println("p = " + string(p))
println("s = " + s)
}
output:
go build -gcflags='-m=2' main.go
# command-line-arguments
./main.go:3:6: can inline fn1 as: func() int { s := 1 + 2; return s }
./main.go:16:6: cannot inline fn2: function too complex: cost 81 exceeds budget 80
./main.go:47:6: cannot inline fn3: function too complex: cost 85 exceeds budget 80
I also tried -gcflags=-m=3, -gcflags=-m=4, -gcflags=-m=5
, the results is like below, very hard to understand. Maybe somebody can help me point out a direction.
go build -gcflags=-m=3 main.go
# command-line-arguments
./main.go:3:6: can inline fn1 as: func() int { s := 1 + 2; return s }
./main.go:8:6: cannot inline fn2: recursive
./main.go:16:6: can inline fn3 as: func() { p := fn1(); p = fn2(p); println("p = " + string(p)) }
./main.go:17:10: inlining call to fn1 func() int { s := 1 + 2; return s }
./main.go:17:10: Before inlining:
. CALLFUNC l(17) tc(1) int
. . NAME-inlining.fn1 l(3) x(0) class(PFUNC) tc(1) used FUNC-@0
substituting name
. NAME-inlining.s g(2) l(4) x(0) class(PAUTO) tc(1) used int ->
. NAME-inlining.s l(4) x(0) class(PAUTO) tc(1) used int
substituting name
. NAME-inlining.s g(2) l(4) x(0) class(PAUTO) tc(1) used int ->
. NAME-inlining.s l(4) x(0) class(PAUTO) tc(1) used int
substituting name
. NAME-inlining.s g(2) l(4) x(0) class(PAUTO) tc(1) used int ->
. NAME-inlining.s l(4) x(0) class(PAUTO) tc(1) used int
./main.go:17:10: After inlining
. INLCALL-init
. . DCL l(17)
. . . NAME-inlining.~r0 l(3) x(0) class(PAUTO) tc(1) assigned used int
. . AS l(17) tc(1)
. . . NAME-inlining.~r0 l(3) x(0) class(PAUTO) tc(1) assigned used int
. . INLMARK l(+17) x(0)
. INLCALL l(17) tc(1) int
. INLCALL-rlist
. . NAME-inlining.~r0 l(3) x(0) class(PAUTO) tc(1) assigned used int
..... // too much
$ go build -gcflagsUsed to pass flags to the Go compiler. go tool compile -help lists all the flags that can be passed to the compiler. For example, to disable compiler optimizations and inlining, you can use the following the gcflags.
From the command line in the hello directory, run the go build command to compile the code into an executable. From the command line in the hello directory, run the new hello executable to confirm that the code works.
go run is just a shortcut for compiling then running in a single step. While it is useful for development you should generally build it and run the binary directly when using it in production. There must be more to it than that - if I time the println in a helloworld, it runs faster with go run than compiled.
go build command is generally used to compile the packages and dependencies that you have defined/used in your project. So how go build is executing internally, what compiler executes, which directories created or deleted; Those all questions are answered by go build command flags.
-gcflags
flag accepts list of flags, that should be passed to go tool compile
when it's invoked. So you can see all possible options in its documentation or running command:
go tool compile -help
add -help at the end:
go build -gcflags -help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With