Details of what effect inline
has are specific to a compiler. Compiler-specific extensions such as __attribute__((always_inline))
or __forceinline
might be more useful as they have stronger and more predictable effect.
For example with Clang, the function gets inlined if its "cost" is lower than the threshold value. The threshold value is set to 225 for function without any inline
attributes, but is multiplied by 0.333, 1.500 or 1.667 for optimization levels -Os
, -O2
and -O3
respectively. With inline
, the threshold value is set to 487. So the effect is more noticeable when compiling with lower levels, especially when using -Os
(75 vs 487 threshold). You can check and experiment with Clang's "Optimization Output" at https://godbolt.org.
Compilers do not require inline
keyword to inline the function. Although there are still situation when inlining will be disabled for some functions, unless inline
keyword is used. For example GCC won't inline functions with "default" visibility on ELF targets when building shared objects (-fPIC
flag) unless -fno-semantic-interposition
is specified.
The most common advise will be to "profile" the code, but I don't find it very useful as you won't be able to check all possible combinations of inline
keyword on all functions. It all comes with experience: C programmers usually know how the code generated by the compiler will look like, they also know what code they want to get in the end.
Currently, main goals of inline
in C are:
- To suppress warnings for unused
static inline
functions (that come from headers)
- To implement
extern inline
functions.
Clang inlining can be manipulated through LLVM parameters -inline-threshold
and -inlinehint-threshold
.
In C++ inline
has additional meaning - inline functions generate "weak" symbols, duplicates of which have to be removed by the linker. In some cases function are treated as marked with inline
implicitly, but only semantically. Further adding inline
keyword increases inlining threshold at least on Clang (from a normal level to an "inline" level).