c++ inline浅析
gcc
提供关键词 inline
,允许我们标注函数需要在编译时展开,这样可以避免函数调用,减低函数栈开销,从而达到优化程序的目地。然而,天下并没有免费的午餐,在引入 inline
的同时,我们也需要注意其带来的程序体积变大、cache locality
减少的风险。
Pros of inlining:
- Removes function call overhead (yay!)
- May reveal additional optimization opportunities (sometimes yay!)
Cons of inlining:
- Increases program size (boo!)
- May reduce cache locality (sometimes boo!)
- May increase build times (boo!)