我正在通过内联学习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

最佳答案

-gcflags标志接受标志列表,调用时应将其传递给go tool compile。因此,您可以在it's documentation或运行命令中查看所有可能的选项:

go tool compile -help

07-26 09:35