Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pros and cons of 'inline'

Tags:

c++

First of all, I would like to state the facts I know about 'inline', so that you don't bother to restate them.

  1. An inline function is a special kind of function whose definition must be available in every translation unit in which the function is used.
  2. It is a hint to the compiler (which it is free to ignore) to omit the function call, and expand the body instead of the call.
  3. The only pro I know of is that (2.) may make the code faster.
  4. The only con I know if is that (1.) increases coupling which is bad.

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.

like image 253
Armen Tsirunyan Avatar asked Oct 08 '10 07:10

Armen Tsirunyan


People also ask

What is the advantages and disadvantages of inline function?

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.

What are the disadvantages of inline 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.

Are inline functions faster?

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.

What is the purpose of inline function?

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.


2 Answers

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.

like image 150
greyfade Avatar answered Sep 26 '22 04:09

greyfade


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).

like image 24
Alexandre C. Avatar answered Sep 22 '22 04:09

Alexandre C.