When you mark a function as inline
, you hint the compiler that this function is a candidate for inlining. The compiler can still decide that it's not a good idea, and ignore it.
Is there a way to see if the function gets inlined or not, without using the disassembler? Is there some compiler warning that I don't know about maybe?
What are the rules for inlining that the compiler uses? Are there constructs that cause a function to never get inlined for example?
The decision to inline or not a function is made by compiler. And since it is made by compiler, so YES, it can be made at compile time only. So, if you can see the assembly code by using -S option (with gcc -S produces assembly code), you can see whether your function has been inlined or not.
An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.
An std::function cannot be inlined This overhead can have an impact on the performance if small functions wrapped in std::function are being called very frequently.
Inline function expansion can speed up execution by eliminating function call overhead. This is particularly beneficial for very small functions that are called frequently. Function inlining involves a tradeoff between execution speed and code size, because the code is duplicated at each function call site.
The compiler emits a hint if it can't inline your function. The documentation explains the rules for what can and cannot be inlined.
As for the discretionary decisions that the compiler takes as to whether or not to inline (as opposed to whether or not inlining is possible), they are not documented and can be considered an implementation detail.
I recall that you recently commented on one of my answers to a different question that a particular function was 10 times faster once inlined. Clearly you are interested in inlining but in that particular case I cannot believe such an enormous gain for a function with so many floating point operations. I suspect that inlining is not actually giving you the performance improvements that you think it does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With