r/C_Programming • u/wiseneddustmite • May 31 '26
question about inline
i read that inline tells the compiler to write the code of the function directly where the function is called in the code instead of calling the function that was declared separately and this saves a bit of performance, but when should/shouldn't inline be used
9
Upvotes
20
u/EpochVanquisher May 31 '26
The compiler will ignore inline according to its own rules for whether a function should or should not be inlined. In fact, the compiler will often choose to inline functions which aren’t declared inline, and will often choose to not inline functions which are declared inline.
So it is mostly harmless, when used correctly.
The main thing that
inlinedoes is that it makes inlining possible for certain functions if you have compiler optimizations enabled but linker optimizations turned off. Use it for small functions in header files. There is no point in using it for functions which are already declaredstatic.3
u/No-Duck-6055 May 31 '26
If you have lengthy functions which are called a lot, frequent use of inline would cause your code to bloat significantly.
3
u/mykesx May 31 '26 edited May 31 '26
It may provide a hint to the compiler, though it is not guaranteed to inline the function. It may also be good for someone reading the code as to your intention.
An obvious use might be a plot_pixel() function that you use in nested loops. The call/return overhead plus the loops overhead likely kill performance.
3
u/QuirkyXoo Jun 01 '26
As a general rule, don't inline anything that doesn't fit on a single line.
Modern compilers simply laugh at your inline declarations because they follow their own heuristics, especially when you ask them to optimize the code.
With MSVC, the only way to force the compiler to inline a function is by using the __forceinline keyword.
3
u/Ilike_milk May 31 '26
If the inline function is large and you call it a lot, the executable can grow in size. So for embedded systems where there isn’t a lot of space you’ll have to make sure it can actually fit
1
u/Erdnuss2562 May 31 '26
How the "inline" keyword is treated by the compiler is - as the standard puts it - "implementation defined", i.e., its totally up to the compiler whether to respect the request to inline or not. I've seen compilers which will take the "inline" literally and always inline, others completely ignore it (but may offer some compiler extension, e.g. an __attribute__ to inline forcefully).
As others suggested: If you think a function should be inlined by the compiler, the recommendation is to make it "static" and let the compiler work out for itself. I would trust modern compilers to inline the function whey they are small, or just used once and for anything in between have a good heuristic (depending on whether you optimize for speed or size) whether to inline or not.
1
u/chibuku_chauya May 31 '26
I always use this site as a reference about the semantics of
inline. Note that there are two flavours ofinline: GNU and C99.1
u/pjl1967 Jun 01 '26
Two articles that tell you everything you need to know about
inline:1
u/flyingron Jun 01 '26
You either read an incorrect book or you misunderstood it.
Inline does TWO things:
1
u/marcthe12 May 31 '26
Well inline increase the amount of code size. This has the effect of bigger executables and worst instruction cache in cpu. Pretty much all the arguements for Os comes against inline. Note there is a difference between static inline and plain inline. Plain inline signals inline if posible else import from another TU. Static force it to be inlined.
Now personal inline is good if the code is small or has room for specialization. Often if looping an array param, the optimizer can easily vectorize if it the size is constant. Or DCE at time. But if your code is too big (pretty when plain inline emits a symbol size), it's become code bloat.
0
u/runningOverA May 31 '26
"inline" was a thing for the 90s.
now ignore it. compiler will inline without you telling it.
in 2020s : rather focus on "static".