[NSWI004] static inline

Lubomír Bulej bulej at d3s.mff.cuni.cz
Tue Nov 10 14:55:52 CET 2020


Hi,

TLDR: they are normal functions, except they are very likely to be inlined.


Each of the modifiers tells the compiler something about the function.

"static" tells the compiler that the function is private to the compilation
unit, i.e., it cannot be called from outside the file it is used to.

"inline" is a hint for the compiler suggesting to inline the function when
called. That keyword is relatively old and as compiler technology advanced,
the compilers were typically able to make better decisions about whether to
inline a function or not, especially for functions that are "public", i.e.,
exported. Modern compilers are therefore likely to ignore the hint on
non-static functions.

When used together, "static inline" tells the compiler that a function is
local to a compilation unit (i.e., cannot be called from outside) and that it
would be nice to inline it, which the compilers (even modern ones) are much
more likely to respect. The function then effectively becomes a snippet of
code that can be inlined whenever needed.

This allows removing function call overhead for functions that just hide
implementation details, but are otherwise so short that the function call
overhead would be comparable to what the function is actually doing.

For example, in the case of the list implementation, many of the operations
just rewire pointers, so they compile to a few (small units) instructions.

This is one of the few cases (at least in C) in which code is allowed to live
in a header file, because it is meant to provide a private copy of the code to
any source file that uses it. The compiler throws away the unused functions
(they are all static), and inlines those that are used, thanks to the "static
inline" hint combined with small size of those functions.



Best regards,
Lubomir


On 10/11/2020 13:11, Káně, Vojtěch wrote:
> Hello,
> I would like to ask what exactly static inline functions are. They are used in
> include/adt/list.h for example. I did some research on my own, but all the
> answers were mixing ANSI C, GNU C and C++ together so I got confused even more.
> 
> I am asking just out of curiosity, it is not blocking my implementation.
> 
> Thank you in advance,
> Vojtěch Káně
> 



More information about the NSWI004 mailing list