I know that inline is a hint or request to compiler and its used to avoid function call overheads.
So on what basis one can determine whether a function is a candidate for inlining or not ? In which case one should avoid inlining ?
I know that inline is a hint or request to compiler and its used to avoid function call overheads.
So on what basis one can determine whether a function is a candidate for inlining or not ? In which case one should avoid inlining ?
The best way would be to examine and compare the generated instructions for inlined and not inlined. However, it is always safe to omit
inline
. Usinginline
could lead to trouble you don't want.Also, an inline method has severe side effects when maintaining large projects. When the inline code is changed, all files that use it will be rebuild automatically by the compiler (it it is a good compiler). This could waste a lot of your development time.
When an
inline
method is transferred to a source file and not inlined any more, the whole project must be rebuilt (at least this has been my experience). And also when methods are converted to inline.When deciding on whether to use inline, I usually keep the following idea in mind: On modern machines memory latency can be a bigger bottleneck than raw calculations. Inlining functions that are called often is known to grow the executable size. Furthermore, such a function could be stored in the CPU's code cache which will decrease the number of cache misses when that code needs to be accessed.
Hence, you have to decide for yourself: Does inlining increase or decrease the size of the generated machine code? How likely is it that calling the function will cause a cache miss? If it is peppered throughout the code, then I would say the likelihood is high. If it is restricted to a single tight loop then the likelihood is hopefully low.
I typically use inlining in the cases I list bellow. However, where you are genuinely concerned about performance, profiling is essential. Furthermore, you might want to check whether the compiler actually takes the hint.
I generally follow a thumb rule where I make a function with 3-4 simple statements as inline. But it is good to remember that it is just a hint to the compiler. The final call to make it inline or not is taken by the compiler only. If there are more than these many statements I will not declare inline as with a stupid compiler it may lead to code bloat.
I have read some answers and see that there some stuff missing.
The rule I use is not to use inline, unless I want it to be inline. Looks silly, now explanation.
Compilers are smart enough and short functions always makes inline. And never makes long function as inline, unless programmer said to do that.
Actually
inline
is an order for compiler, it has no choices and afterinline
keyword makes all code inline. So you can never useinline
keyword and compiler will design shortest code.So when to use
inline
?To use if you want to have some code inline. I know only one example, because I use it in only one situation. It is user authentication.
For example I have this function:
No matter how big this function is I want to have it as inline because it makes my software harder to crack.
Inline functions might improve your code performance by eliminating the need to push arguments into the stack. if the function in question is in a critical part of your code you should make the inline not inline decision in the optimization part of your project,
you can read more about inlines in the c++ faq