7

If you've enabled full optimizations in your compiler and have classes setup like this:

class A
{
    void Do_A_Stuff();
};
class B
{
    A a;
    void Do_B_Stuff() { a.Do_A_Stuff(); }
};
class C
{
    B b;
    void Do_C_Stuff() { b.Do_B_Stuff(); }
};
class D
{
    C c;
    void Do_D_Stuff() { c.Do_C_Stuff(); }
};

Is there ever a situation where calling Do_D_Stuff() would be slower than directly calling Do_A_Stuff()? Also, would this require the inline keyword on each wrapper 'chain' or, since it is only a suggestion, could the compiler decide to optimize this without the keyword?

I realize there is a lot of information about inlining available, but I could not find any information specifically about chaining many wrappers together.

Jonathan
  • 752
  • 1
  • 9
  • 19

2 Answers2

8

Also, would this require the inline keyword on each wrapper 'chain' or, since it is only a suggestion, could the compiler decide to optimize this without the keyword?

Yes, the compiler could decide to optimize it anyway, and it could also decide not to optimize it even if you specified the inline keyword (possibly producing a warning if the appropriate compiler options are set) - notice, that member functions defined in a class definition are implicitly marked as inline.

In general, if inlining is possible, the compiler will decide whether to inline or not based on the body of the function being called. However, inlining may not be possible at all if the function is a virtual function, or if the definition of the function is not visible to the compiler.

Provided that the conditions for inlining are satisfied and that the compiler considers it appropriate, there is no technical problem in inlining over a chain of function calls.

As a minor remark, notice that the functions in your classes should be public, otherwise they won't be accessible to your wrappers.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Yes, the functions should be `public`, I was going for brevity. :) – Jonathan Mar 28 '13 at 23:43
  • *"or if the definition of the function is not visible to the compiler."* - does this apply to [link time code generation](http://msdn.microsoft.com/en-us/library/xbf3tbeh(v=vs.80).aspx) in MSVC (or equivalent in other compilers)? I've always been curious about that. – JBentley Mar 28 '13 at 23:44
  • 1
    @JBentley: Cross-module inlining is also possible, yes. There is also [this relevant Q&A](http://stackoverflow.com/questions/5987020/can-the-linker-inline-functions) that you can check. Thank you for your remark – Andy Prowl Mar 28 '13 at 23:47
  • @AndyProwl I just want to clarify one more thing before I mark this as the answer, if ALL of the functions in my example were inlined, would a call to `Do_D_Stuff()` basically be directly running the code within `Do_A_Stuff()` once compiled? (I'm trying to figure out if the end result is skipping the 'overhead' of multiple wrappers.) Thanks! – Jonathan Mar 29 '13 at 00:35
  • 1
    @Jonathan: Yes, that's what would happen – Andy Prowl Mar 29 '13 at 00:37
  • Virtual functions may be inlinable if the compiler can statically deduce the concrete type of which a member is being invoked. – Nathan Ernst Mar 29 '13 at 01:09
  • @NathanErnst: Indeed. That's why I wrote "may" :) – Andy Prowl Mar 29 '13 at 08:10
0

The functions are defined inside the class definition, so the inline keyword is implicit in this case.

Oswald
  • 31,254
  • 3
  • 43
  • 68
  • 1
    It should be noted that even using `inline` outside the class definition is no guarantee that the compiler will `inline` your code. It is up to the compiler to decide. – Tushar Mar 28 '13 at 23:39
  • Compilers may inline functions not marked as `inline`, and not inline functions marked as `inline`. – GManNickG Mar 29 '13 at 00:19