c++inlinecompiler-optimization

what are the effects of inline?


I had a discussion with Johannes Schaub regarding the keyword inline. The code there was this:

namespace ... {
    static void someFunction() {
        MYCLASS::GetInstance()->someFunction();
    }
};

He stated that:

Putting this as an inline function may save code size in the executable

But according to my findings here and here it wouldn't be needed, since:

Johannes states that there are other benefits of explicitly specifying it, which I do not understand. For instance, he stated that

[..] "inline" allows you to define the function multiple times in the program.

.. which I am having a hard time understanding (and finding references to).

So

  1. Is inline just a recommendation for the compiler?
  2. Should it be explicitly stated when you have a small function (I guess 1-4 instructions?)
  3. What other effects are there when writing inline?
  4. is it needed to state inline in order to reduce the executable file size, even though the compiler should find such functions itself?

Is there anything else I am missing?


Solution

  • Is inline just a recommendation for the compiler?

    Yes.

    7.1.2 Function specifiers

    2 A function declaration (8.3.5, 9.3, 11.4) with an inline specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected.

    For example from MSDN:

    The compiler treats the inline expansion options and keywords as suggestions. There is no guarantee that functions will be inlined. You cannot force the compiler to inline a particular function, even with the __forceinline keyword. When compiling with /clr, the compiler will not inline a function if there are security attributes applied to the function.

    Note though:

    3.2 One definition rule

    3 [...]An inline function shall be defined in every translation unit in which it is used.

    4 An inline function shall be defined in every translation unit in which it is used and shall have exactly the same definition in every case (3.2). [ Note: a call to the inline function may be encountered before its definition appears in the translation unit. —end note ] If the definition of a function appears in a translation unit before its first declaration as inline, the program is ill-formed. If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required. An inline function with external linkage shall have the same address in all translation units. A static local variable in an extern inline function always refers to the same object. A string literal in the body of an extern inline function is the same object in different translation units. [ Note: A string literal appearing in a default argument expression is not in the body of an inline function merely because the expression is used in a function call from that inline function. —end note ] A type defined within the body of an extern inline function is the same type in every translation unit.

    [Note: Emphasis mine]

    A TU is basically a set of headers plus an implementation file (.cpp) which leads to an object file.

    Should it be explicitly stated when you have a small function (I guess 1-4 instructions?)

    Absolutely. Why not help the compiler help you generate less code? Usually, if the prolog/epilog part incurs more cost than having it inline force the compiler to generate them? But you must, absolutely must go through this GOTW article before getting started with inlining: GotW #33: Inline

    What other benefits are there with writing inline?

    Is it needed to state inline in order to reduce the executable file size, even though the compiler (according to wikipedia [I know, bad reference]) should find such functions itself?

    Again, as I said, as a good programmer, you should, when you can, help the compiler. But here's what the C++ FAQ has to offer about inline. So be wary. Not all compilers do this sort of analysis so you should read the documentation on their optimization switches. E.g: GCC does something similar:

    You can also direct GCC to try to integrate all “simple enough” functions into their callers with the option -finline-functions.

    Most compilers allow you to override the compiler's cost/benefit ratio analysis to some extent. The MSDN and GCC documentation is worth reading.