2

The Named Parameter Idiom as described here mentions that there will be a performance impact when not using inline.

Since each member function in the chain returns a reference, there is no copying of objects and the chain is highly efficient. Furthermore, if the various member functions are inline, the generated object code will probably be on par with C-style code that sets various members of a struct. Of course if the member functions are not inline, there may be a slight increase in code size and a slight decrease in performance (but only if the construction occurs on the critical path of a CPU-bound program; this is a can of worms I’ll try to avoid opening), so it may, in this case, be a tradeoff for making the code more reliable.

Does this still hold true in spite of modern compilers and using -O2 when compiling? I was under the impression, that modern compilers will generally inline on their own (especially with -O2) when they see a possible benefit. In addition the inline keyword is little more than a hint to the compiler and by no means forces the inlining.

2 Answers 2

2

You need to separate inlining optimizations from the C++ inline keyword.

  • Inlining optimizations are possible only if the target function is inline, or has internal linkage. And the call must not be virtual. This is independent from optimization levels because this is about fundamental language semantics.

  • The inline keyword indicates that the function definition must be visible in any compilation unit from which the function is called, and that ODR is disabled for this function: there can be multiple definitions without causing a linker error.

  • Member function definitions within a class definition are implicitly inline. This is inline: struct Foo { void bar() {...} }; this is not: struct Foo { void bar(); }; void Foo::bar() {...}.

  • Functions with internal linkage are only visible in the current compilation unit, and can therefore be optimized more aggressively. A function has internal linkage if a non-member function is marked static (as in C), or if the function is defined in an anonymous namespace.

  • A (static) linker could inline functions during link-time optimization even without the previous cases applying. But few projects use LTO.

If you want to avoid extra overhead from the named parameter idiom, you will want the compiler to do inlining optimization. In that case, you must ensure one of the above cases. C++ compilers do not guarantee any inlining optimizations though, unless you use non-portable compiler-specific attributes.

If you find the overhead acceptable, then you don't have to enable inlining.

0

Inline and inline have multiple, overlapping meanings.

Confusingly, it is suggesting that there would tend to be a performance impact if the compiler chooses not to inline, not if the programmer chooses not to inline.

I presume the example uses inline explicitly because the author prefers defining member functions out-of-line, rather then inline (in the class definition).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.