4 ms·
You can see the reason actually: go build -gcflags="-m -m" . # _/tmp/inline ./main.go:7:6: cannot inline sum: unhandled op RANGE ./main.go:15:6: can in
by Thaxll 6y ago
You can see the reason actually:
go build -gcflags="-m -m" .
# _/tmp/inline
./main.go:7:6: cannot inline sum: unhandled op RANGE
./main.go:15:6: can inline fun as: func() uint64 { x :=
[]uint64 literal; return sum(x) }
./main.go:3:6: can inline main as: func() { _ = fun() }
./main.go:4:9: inlining call to fun func() uint64 { x
:= []uint64 literal; return sum(x) }
./main.go:7:10: sum x does not escape
./main.go:4:9: main []uint64 literal does not escape
./main.go:16:15: fun []uint64 literal does not escape
package main
func main() {
_ = fun()
}
func sum(x []uint64) uint64 {
var sum = uint64(0)
for _, v := range x {
sum += v
}
return sum
}
func fun() uint64 {
x := []uint64{10, 20, 30}
return sum(x)
}
Go does not inline range operation.
- uryga 6y ago> Go does not inline range operation. do you know why? [not super familiar with Go, just curious]
- Thaxll 6y agoI'm not sure I think it's just a current limitation: https://github.com/golang/go/wiki/CompilerOptimizations#function-inlining https://github.com/golang/go/wiki/CompilerOptimizations#func... They do improve the compiler every release, so we might see some of those operations get inlined in the future. More information there: https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/inl.go#L10 https://github.com/golang/go/blob/master/src/cmd/compile/int...