1

Linux kernel developers massively use the inline type qualifier in Linux kernel. If used well, it can reduce the cost of a function call. If not good, it could enlarge image size too much, which increases the cost of memory accessing.

How do they judge when to use the inline type qualifier?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I would suggest to rely on compiler optimisations. Option `-finline-functions` is enabled with `-O2, -O3, -Os` and that would manage this for us in a better way. Here you can read more about it : [Optimisation Flags](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html). – Mihir Luthra Mar 15 '20 at 14:58
  • I would suggest reading this: https://isocpp.org/wiki/faq/inline-functions – Alex Sveshnikov Mar 15 '20 at 15:26
  • @Alex The link focuses on C++ while the question is about C. There are major differences between the two wrt `inline`. –  Mar 15 '20 at 15:30

1 Answers1

4

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).

  • 1
    "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." <= This. +1 – Petr Skocik Mar 15 '20 at 15:40