gpt4 book ai didi

c++ - 编译器如何决定是否值得让我的函数内联?

转载 作者:可可西里 更新时间:2023-11-01 18:26:49 25 4
gpt4 key购买 nike

我想知道是否有人知道 C++ 编译器通常用来决定是否在编译时内联函数的逻辑(假设已请求内联)。

这种事情是公共(public)知识吗?

最佳答案

我给出了更彻底的答案in this other question .

基本上编译器有基于成本分析的启发式,引用我自己的话(这是否意味着我要老了?)

If you think about inlining, and its consequences, you'll realise it:

  • you avoid a function call (with all the register saving/frame adjustment)
  • you expose more context to the optimizer (dead stores, dead code, common sub-expression elimintation...)
  • at the cost of duplicating code (bloating the instruction cache and the executable size, among other things)

当然,还有部分内联,其中只有部分函数是内联的,通常是前导if。守卫喜欢foo(T* t) { if (!t) { return; } <many many things> } .

关于c++ - 编译器如何决定是否值得让我的函数内联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7671866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com