我正在学习Go语言的内存优化,下面的代码是我的测试代码。
我使用go build -gcflags=-m=2 main.go
来得到所有的结果。我遵循Dave Cheney's post来做这件事。
我的问题是:gcflags的值是什么?文档在哪里,我在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)
}
输出:
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
我也试过-gcflags=-m=3, -gcflags=-m=4, -gcflags=-m=5
,结果如下,很难理解。也许有人能给我指出一个方向。
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
2条答案
按热度按时间ogq8wdun1#
-gcflags
flag接受一系列的标记,当它被调用时,应该被传递给go tool compile
。所以你可以看到its documentation或running命令中所有可能的选项:f1tvaqid2#
并运行:
示例: