First of all, I would like to state the facts I know about 'inline', so that you don't bother to restate them.
Now let's consider templates. If I have a template library, I need to provide the definitions of the function templates in every translation unit, right? Let's forget about the controversial 'export' for a while, since it doesn't really solve the problem anyway. So, I come to a conclusion that there is no reason not to make a template function inline because the only con of inline I know of is there a priori.
Please correct me if I am wrong. Thanks in advance.
1) Function call overhead doesn't occur. 2) It also saves the overhead of push/pop variables on the stack when function is called. 3) It also saves overhead of a return call from a function. 4) When you inline a function, you may enable compiler to perform context specific optimization on the body of function.
Disadvantages of Inline Function in C++Excess use of inline functions in the code may lead to the enlargement of the size of the binary executable code. In the case of large functions, compilation overhead can take place and reduce the efficiency of the code.
inline functions might make it faster: As shown above, procedural integration might remove a bunch of unnecessary instructions, which might make things run faster. inline functions might make it slower: Too much inlining might cause code bloat, which might cause “thrashing” on demand-paged virtual-memory systems.
An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.
The only pro I know of is that (2.) may make the code faster.
May being the operative word. Inlined functions may make certain code paths faster, yes.
But an inlined function puts additional pressure on the instruction cache on most modern CPUs. If your function is too large to fit in the L1 instruction cache, it may actually run slower than performing a function call (for which the CPU can optimize by prefetching the function and its return site).
Inlining a function may also put undue pressure on the L2 cache - if an inlined function is used an unusually large number of times, the extra code size will increase the likelihood of cache misses, leading to long delays and pipeline stalls as the CPU twiddles its thumbs waiting for the memory bus to do something.
inline
is far from being a silver bullet. Compilers with aggressive optimization will ignore the inline
hint completely, as they will instead choose functions to inline based on heuristics such as code size or the presence of branches, irrespective of the presence or absence of the inline
keyword.
The only con I know if is that (1.) increases coupling which is bad.
This is something I've never heard. "Coupling" is a concept I've only heard used when describing the high-level relationships of code. It's more an issue of maintainability and generality of code. inline
is an issue of low-level code generation.
As to templates, again, an aggressively-optimizing compiler will inline if its heuristics show an advantage to doing so.
There is, however, a link-level issue to consider: You may need to declare a function or template inline
(or static
, depending on the situation) in order to eliminate duplicate symbols at link time or restrict symbol visibility. This is, of course, not an optimization.
In summary, don't bother using the inline
keyword except when specifically required.
I think you shouldn't bother with writing inline
or not before template function definitions. I think most compilers will inline small functions anyway, whether you ask them or not. Some compilers are even able to do this at link time for functions defined in only one translation unit.
Imho, the only point to the inline
keyword is to be able to define non-template functions inside headers, for those compilers who don't inline at link time (or when you want a "header only" library).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With