简体   繁体   中英

The timing of using type qualifier, inline, in Linux kernel

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM