3

From what I know an inline function is an optimization to enhance performance, thus it should run as fast as a macro. Inline function's code should be as short as possible.

I wonder if it make sense to embed functions calls inside an inline function. If the answer is yes, in which context and what are the restrictions?

Actually, I am asking this question because I looked at a code of someone who is calling functions such as "socket()", "sendto()" and "memset()" from inline functions; something that overrides the purpose of an inline function in my opinion.

Note: In the code I have there is no use of any conditional calls to the functions, the inline function just passes arguments to the called functions.

Aymen
  • 261
  • 2
  • 10
  • 1
    inline is a suggestion the compiler is free to disregard it. – Borgleader Jan 12 '15 at 14:30
  • Thanks but I know that. My question is about best practices associated with inline functions and whether or not the guy who wrote the code is doing the right thing. – Aymen Jan 12 '15 at 14:34
  • 1
    It makes perfect sense if those calls are conditional and rarely happen (so the most frequent path bypasses them). If they're not it may make sense anyway, depending on the functions (they may even be inlined; you can't know by looking at source code what the compiler will do to it). Plus, the code you're looking at may not be the greatest reference material for the best use of "inline". – molbdnilo Jan 12 '15 at 14:43
  • Actually, the code I have does not make use of any conditional calls to the embedded functions, it just passes arguments to the called functions. However, it seems from the description you provided that using inline is a win-win game. One can define a function as inline and let the compiler does the optimizations if any. – Aymen Jan 12 '15 at 14:55
  • 1
    @Aymen if the guy who wrote the code believed that `inline` had any meaningful impact on performance, he was manifestly NOT 'doing the right thing'. Performance comes from correct algorithm selection and avoiding cache misses. Headaches come from naive premature optimisation techniques that may have worked in 1991. – Richard Hodges Jan 12 '15 at 15:17
  • Very nice answer Richard :) – Aymen Jan 12 '15 at 15:18
  • @Richard: could you please post your answer so I can vote it as the best answer. – Aymen Jan 12 '15 at 15:25
  • posted as requested :-) – Richard Hodges Jan 12 '15 at 15:28

6 Answers6

4

I wonder if it make sense to embed functions calls inside an inline function.

Of course it does. Inlining a call to your function is still an optimisation, removing the cost of that function call and allowing further optimisations in the context of the calling function, whether or not it in turn calls other functions.

in which context and what are the restrictions?

I've no idea what you mean by "context"; but there are no restrictions on what you can do in an inline function. The only semantic effects of declaring a function inline are to allow multiple identical definitions of the, and require a definition in any translation unit that uses the function. In all other respects, it's the same as any other function definition.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Many materials indicate that one of the main benefits of using inline function is to avoid the overhead caused by calling a function. This is why I found it a bit weird to call two or three functions from an inline function. Nevertheless, I agree with you about the fact there is a gain of one function call in any case. – Aymen Jan 12 '15 at 14:43
2

I see no a priori reason why inline code could not contain function calls.

Argument passing aside, inlining inserts the lines of code as they stand, reducing call overhead and allowing local/ad-hoc optimizations.

For instance, inline void MyInline(bool Perform) { if (Perform) memset(); } could very well be skipped when invoked with MyInline(false).

Inlining could also allow inlining of the internal function calls, resulting in even more (micro)optimization opportunities.

  • Please, could you clarify under which conditions a compiler may decide to inline the called functions as well? Are they exactly the same rules used to inline the calling function? – Aymen Jan 12 '15 at 15:10
  • 1
    @Aymen: this is compiler dependent and subject to change. No rule. –  Jan 12 '15 at 15:15
2

Comment posted as answer, by request:

If the guy who wrote the code believed that inline had any meaningful impact on performance, he was manifestly NOT 'doing the right thing'.

Performance comes from correct algorithm selection and avoiding cache misses.

Headaches come from naive premature optimisation techniques that may have worked in 1991

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • 3
    I know the OP requested you post this as an answer, but I'm not sure why. How does this address the question in the post? – Benjamin Lindley Jan 12 '15 at 15:30
  • @Benjamin: the question is specific to a specific implementation that I have. Some of the answers included some useful information but most of them are spinning around the same idea and how to use inline function which is not that appropriate to my question. I hope that clarifies my choice despite I don't need to give arguments. – Aymen Jan 12 '15 at 15:40
  • @Aymen this answer isn't wrong, but it's far from the best one on this thread. You asked a question based on your perception of inline as a performance enhancement, but it hasn't had *any* impact on performance for years. Not only can the compiler refuse to actually inline a function declared as such, but with [whole program optimization](http://en.wikipedia.org/wiki/Interprocedural_optimization) it can even inline functions that aren't defined in the same translation unit. Your coworker did nothing wrong. – Mark Ransom Jan 13 '15 at 03:39
1

The compiler will choose when to inline. And you should avoid attempting premature optimisation at the expense of exposing your implementation.

The compiler may be able to optimise away the forwarding of the functions you are calling. It might do that anyway with optimisation flags even if you do not use the inline keyword.

The time to use the inline keyword is when you want to make a header-only file to use in multiple projects without having to use a link-library. In reality this doesn't really mean "inline" at all, it means "one definition only" even across compilation units calling the function.

In any case you should look at this wiki question / answer:

Benefits of inline functions in C++?

Community
  • 1
  • 1
CashCow
  • 30,981
  • 5
  • 61
  • 92
1

It makes a perfect sense.

Consider a function that consists of two possible branches of execution — a fast path which is activated when certain condition holds (most of the time) and a slow path.

Inlining the whole thing would result in growing the size of the code for little benefit. The slow path complexity may prevent the compiler from inlining the function.

If you make the slow path into a separate function an interesting opportunity opens.

It makes it possible to inline the condition and the fast path while a slow path remains a function call. Inlining the fast path allows to avoid function call overhead most of the time. The slow path is already slow hence the call overhead is negligible.

Nick Zavaritsky
  • 1,429
  • 8
  • 19
0

First, in C++, an "inline" function (one declared in the header file or labeled as such) is just a suggestion to the compiler. The compiler itself will make the decision on whether or not to actually make it inline.

There are three reasons why to inline a function:

  1. Pushing another round of variables onto the stack is expensive, as is branching to the new point in the program.
  2. Sometimes we can do local optimizations with the intermediates of the function (though I wouldn't count on it!)
  3. Put the definition of a function in the header file (work around).

Take the following example

void NonInlinable(int x);
inline void Inline() { NonInlinable(10);}

This makes a ton of sense to inline. I remove 1 function call, so if NonInlinable is pretty fast, then this could be a huge speedup. So regardless of whether or not I'm calling functions, I could still want to inline the call.

Now another example:

 inline int Inline(int y) {return y/10;}
 //main
 ...
 int x = 5;
 int y = Inline(5);
 int z = x % 10;

The modulo and devise operations are usually calculated by the same instruction. A really nice compiler, can compute y and z in 1 assembly instruction! magic

So in my mind, a better question to ask is when should I not use inline functions:

  1. When I want to separate definition from declaration (very good practice for readability, and premature optimization is the root of all evil).
  2. When I want to hide my implementation/use good encapsulation.
IdeaHat
  • 7,641
  • 1
  • 22
  • 53