Hands-On High Performance Programming with Qt 5
上QQ阅读APP看书,第一时间看更新

Inlining of functions

The first of the unspectacular optimizations is function inlining, which amounts to the copying of the function code into the call site instead of calling it. We basically save the function invocation overhead, increasing the code size as a price. So, how big can the performance gain be, really? You might think that calling a function surely cannot be the big showstopper! With respect, you would be wrong. Inlining a function not only shaves off the function call overhead, it also enables many other optimizations as a result. The gains achieved by inlining can lie in the double-digit percentage range and it has also been reported to be as high as 150%.

However, inlining is a double-edged sword—make too much of it and you can actually slow down your program due to the code bloat! For that reason, compilers provide a fine-grained control for it, with annotations to disable it, such as GCC's __attribute__((noinline)) and Microsoft's __declspec(noinline) , and to force it, that is, __attribute__((always_inline)) for GCC and __forceinline for Microsoft. We also have the inline hint directly in C++, but it's only that—a hint.

These annotations allow us to correct a compiler's work in both directions—both when it gets overzealous and when it gets too parsimonious.