提升go编译器内联程度

栏目: Go · 发布时间: 7年前

内容简介:根据可以使用编译参数我们可以通过添加

根据 Proposal: Mid-stack inlining in the Go compiler ,当前 go1.9+ 已经支持用户调整编译器的内联程度。

可以使用编译参数 go build -gcflags="-l=4" 增加内联程度,从而提升9%的运行速度,代价是生成的二进制程序大小提升11-15%,看起来是很不错的一个交换。

我们可以通过添加 -m=2 参数来对比效果。我们可以使用下列命令来对比前后内联的程度,数据结果越少,表示内联的函数越多。

# 优化前
go build -gcflags="-m=2" 2>&1 | grep "too complex"
# 优化后
go build -gcflags="-m=2 -l=4" 2>&1 | grep "too complex"

根据 cmd/compile/internal/gc/inl.go 源码显示, -l 的取值为:

// The debug['l'] flag controls the aggressiveness. Note that main() swaps level 0 and 1,
// making 1 the default and -l disable. Additional levels (beyond -l) may be buggy and
// are not supported.
//      0: disabled
//      1: 80-nodes leaf functions, oneliners, panic, lazy typechecking (default)
//      2: (unassigned)
//      3: (unassigned)
//      4: allow non-leaf functions

当然,一味的吹求函数内联并不总能带来性能的提高,因为随着代码段大小的提升,CPU运行时 instruction cache miss 会随之增加,过高时会 适得其反 ,所以是否采用还要以实际的PROFILE为准绳。


以上所述就是小编给大家介绍的《提升go编译器内联程度》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Practical Vim, Second Edition

Practical Vim, Second Edition

Drew Neil / The Pragmatic Bookshelf / 2015-10-31 / USD 29.00

Vim is a fast and efficient text editor that will make you a faster and more efficient developer. It’s available on almost every OS, and if you master the techniques in this book, you’ll never need an......一起来看看 《Practical Vim, Second Edition》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码