Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go 内联函数 #49

Open
Petelin opened this issue Nov 18, 2019 · 0 comments
Open

Go 内联函数 #49

Petelin opened this issue Nov 18, 2019 · 0 comments

Comments

@Petelin
Copy link
Owner

Petelin commented Nov 18, 2019

Go 内联函数

Go 有内联编译吗?

Yes it does, but not as aggressive as it could be. For example, only leaf functions can be inlined, i.e. functions that do not call other functions. IIRC also ASM functions are not inlined, and there are some other conditions.
不过有人已经在做更好的内联编译了, 因为这种方式可以提升性能, go1.13 release note里就提到

The fast paths of Mutex.Lock, Mutex.Unlock, RWMutex.Lock, RWMutex.RUnlock, and Once.Do are now inlined in their callers. For the uncontended cases on amd64, these changes make Once.Do twice as fast, and the Mutex/RWMutex methods up to 10% faster.

How to disable inline by Compiler?

just add //go:noinline before the function

how to know it is inline or not ?

run go build --gcflags="-m -m -m" -m 越多信息越多

内联函数的缺点

但在选择使用内联函数时,必须在程序占用空间和程序执行效率之间进行权衡,因为过多的比较复杂的函数进行内联扩展将带来很大的存储资源开支

如果编译器处理的不好, 内联函数debug会更困难, 因为本质上是编译器改了你的代码

@Petelin Petelin changed the title Go 内联function Go 内联编译 Nov 18, 2019
@Petelin Petelin changed the title Go 内联编译 Go 内联函数 Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant