Aussie AI
Inline Functions
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Inline Functions
Placing the keyword “inline
” before any function declarations makes that function instantly disappear
in a puff of smoke.
Well, sort of.
It gives your C++ compiler the hint to optimize the code by putting the function's body
there instead of the function call.
This is faster, but means there are many copies of the function's statements, so it increases code size.
Which functions should you inline? General wisdom is to do so for these types of C++ functions:
- Short functions (esp. single-statement functions)
- Getters and setters in a class
- Frequently called functions at the bottom of the call hierarchy.
The inline
specifier is just a hint.
Your compiler is free to completely ignore you.
In fact, this choice will probably disappear in a few years,
as compilers become better than humans at choosing which functions to inline.
If you want to force the compiler to inline, use preprocessor macros.
However, there's a whole minefield of problems in function-like macros.
For example, you need to add parentheses around the whole expression and also
around each parameter's appearance
in the replacement text.
Hence, inline
functions are much safer than macros.
The value of inline
functions is not only from avoiding function call overhead.
The merging of the statements into the caller's code also allows many
other optimizations to be applied there
as follow-up transformations.
Constants can be propagated further through the inlined statements,
which is similar to constexpr
, but the range of optimizations is much larger with inline
.
GCC has some additional C++ language features related to inlining.
There is the “always_inline
” function attribute which says to always inline this function,
and the “flatten
” attribute which says to inline every call to other functions inside this function.
There is also the “gnu_inline
” attribute that prevents creation of a non-inlined function body.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |