Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline Function (When to insert)?

Tags:

c++

Inline functions are just a request to compilers that insert the complete body of the inline function in every place in the code where that function is used.

But how the compiler decides whether it should insert it or not? Which algorithm/mechanism it uses to decide?

Thanks,

Naveen

like image 451
Naveen Avatar asked Aug 13 '09 11:08

Naveen


2 Answers

Some common aspects:

  • Compiler option (debug builds usually don't inline, and most compilers have options to override the inline declaration to try to inline all, or none)
  • suitable calling convention (e.g. varargs functions usually aren't inlined)
  • suitable for inlining: depends on size of the function, call frequency of the function, gains through inlining, and optimization settings (speed vs. code size). Often, tiny functions have the most benefits, but a huge function may be inlined if it is called just once
  • inline call depth and recursion settings

The 3rd is probably the core of your question, but that's really "compiler specific heuristics" - you need to check the compiler docs, but usually they won't give much guarantees. MSDN has some (limited) information for MSVC.

Beyond trivialities (e.g. simple getters and very primitive functions), inlining as such isn't very helpful anymore. The cost of the call instruction has gone down, and branch prediction has greatly improved.

The great opportunity for inlining is removing code paths that the compiler knows won't be taken - as an extreme example:

inline int Foo(bool refresh = false)
{
   if (refresh)
   {
      // ...extensive code to update m_foo  
   }
   return m_foo;
}

A good compiler would inline Foo(false), but not Foo(true).

With Link Time Code Generation, Foo could reside in a .cpp (without a inline declararion), and Foo(false) would still be inlined, so again inline has only marginal effects here.


To summarize: There are few scenarios where you should attempt to take manual control of inlining by placing (or omitting) inline statements.

like image 152
peterchen Avatar answered Sep 30 '22 04:09

peterchen


The following is in the FAQ for the Sun Studio 11 compiler:

The compiler generates an inline function as an ordinary callable function (out of line) when any of the following is true:

  • You compile with +d.
  • You compile with -g.
  • The function's address is needed (as with a virtual function).
  • The function contains control structures the compiler can't generate inline.
  • The function is too complex.

According to the response to this post by 'clamage45' the "control structures that the compiler can't generate inline" are:

  • the function contains forbidden constructs, like loop, switch, or goto

Another list can be found here. As most other answers have specified the heuristics are going to be 100% compiler specific, from what I've read I think to ensure that a function is actually inlined you need to avoid:

  • local static variables
  • loop constructs
  • switch statements
  • try/catch
  • goto
  • recursion
  • and of course too complex (whatever that means)
like image 28
Richard Corden Avatar answered Sep 30 '22 06:09

Richard Corden