Say I have some functions, each of about two simple lines of code, and they call each other like this: A
calls B
calls C
calls D
… calls K
. (So basically it's a long series of short function calls.) How deep will compilers usually go in the call tree to inline these functions?
C++ – How Deep Do Compilers Inline Functions
c++compiler-constructionfunctioninline
Best Answer
The question is not meaningful.
If you think about inlining, and its consequences, you'll realise it:
When deciding whether to inline or not, the compiler thus performs a balancing act between the potential bloat created and the speed gain expected. This balancing act is affected by options: for gcc
-O3
means optimize for speed while-Oz
means optimize for size, on inlining they have quasi opposite behaviors!Therefore, what matters is not the "nesting level" it is the number of instruction (possibly weighted as not all are created equal).
This means that a simple forwarding function:
is essentially "transparent" from the inlining point of view.
One the other hand, a function counting a hundred lines of code is unlikely to get inlined. Except that a
static
free functions called only once are quasi systematically inlined, as it does not create any duplication in this case.From this two examples we get a hunch of how the heuristics behave:
After that, they are parameters you should be able to set to influence one way or another (MSVC as
__force_inline
which hints strongly at inling,gcc
as they-finline-limit
flag to "raise" the treshold on the instruction count, etc...)On a tangent: do you know about partial inlining ?
It was introduced in gcc in 4.6. The idea, as the name suggests, is to partially inline a function. Mostly, to avoid the overhead of a function call when the function is "guarded" and may (in some cases) return nearly immediately.
For example:
could get "optimized" as:
Of course, once again the heuristics for inlining apply, but they apply more discriminately!
And finally, unless you use WPO (Whole Program Optimization) or LTO (Link Time Optimization), functions can only be inlined if their definition is in the same TU (Translation Unit) that the call site.