Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no standard way to force inline in C++?

According to the wikipedia C++ article

C++ is designed to give the programmer choice, even if this makes it possible for the programmer to choose incorrectly.

If it is designed this way why there is no standard way to force the compiler to inline something even if I might be wrong?

Or I can ask why is inline keyword is just a hint?

I think I have no choice here.

In the OOP world we call methods on the objects and directly accessing members should be avoided. If we can't force the accessors to be inlined, then we are unable to write high performance but still maintainable applications.

(I know many compilers implement their own way to force inlining but it's ugly. Using macros to make inline accessors on a class are ugly too.)

Does the compiler always do it better than the programmer?

like image 214
Calmarius Avatar asked May 24 '11 09:05

Calmarius


People also ask

Can a function be forced as inline?

There's no guarantee that functions will be inlined. You can't force the compiler to inline a particular function, even with the __forceinline keyword.

Is inline deprecated?

warning. The inline modifier for inline classes is deprecated.

Why would a compiler decide not inline a function?

If the compiler decides that inlining the function will make the code slower, or unacceptably larger, it will not inline it. Or, if it simply cannot because of a syntactical dependency, such as other code using a function pointer for callbacks, or exporting the function externally as in a dynamic/static code library.

Does the compiler automatically inline?

Automatic function inlining and static functions At -O2 and -O3 levels of optimization, or when --autoinline is specified, the compiler can automatically inline functions if it is practical and possible to do so, even if the functions are not declared as __inline or inline .


2 Answers

How would a compiler inline a recursive function (especially if the compiler does not support Tail-call optimization and even if it does, the function is not Tail-call optimize-able).

This is just one reason where compiler should decide whether inline is practical or not. There can be others as well which I cant think of right now.

like image 177
Aamir Avatar answered Oct 19 '22 11:10

Aamir


Does the compiler always do it better than the programmer?

No, not always... but the programmer is far more error prone, and less likely to maintain the optimal tuning over a span of years. The bottom line is that inlining only helps performance if the function is really small (for at least one common/important code path) but then it can help by about an order of magnitude, depending on many things of course. It's often impractical for the programmer to assess let alone keep a careful eye on how trivial a function is, and the thresholds can vary with compiler implementation choices, command line options, CPU model etc.. There are so many things that could suddenly bloat a function - any non-builtin type can trigger all sorts of different behaviours (esp in templates), use of an operator (even new) can be overloaded, the verbosity of calling conventions and exception-handling steps aren't generally obvious to the programmer.

The chances are that if the compiler isn't inlining something that's small enough for you to expect a useful performance improvement if it was inlined, then the compiler's aware of some implementation issue you're not that would actually make it worse. In those gray cases where the compiler might go either way and you're just over some threshold the performance difference isn't likely to be significant anyway.

Further, some programmers (myself included) can be lazy and deliberately abuse inline as a convenient way to put implementation in a header file, getting around the ODR, even though they know those functions are large and that it would be disastrous if the compiler (were required to) actually inline them. This doesn't preclude a forced-inline keyword/notation though... it just explains why it's hard to change the expectations around the current inline keyword.

like image 21
Tony Delroy Avatar answered Oct 19 '22 12:10

Tony Delroy