I am making a Qt application and as I was coding, I took the habit of defining my slots in the header. I found it was easier for me to develop that way though I still define normal functions in the .cpp (unless the function is really small). But now there are some worries from my colleague that putting these in the header is bad practice because the fact of defining them in the header makes them inline so I am looking into the matter to understand everything that is going on. This is the reason I was given:
“Even in-lined functions (other than as required by classes) is a highly debatable practice. In theory, it creates faster, but larger code (avoids function calls and returns by duplicating code). However, several people have noticed that often using in-lining actually creates slower code. The reason why is because it can cause the code to get larger and exceed the size of what fits in one or more caches used at run-time. As a result it causes portions of the function to go in and out of cache every pass through some loop and the cache misses and subsequent reloads are far more costly than a function call to something already in another cache page. It’s an interesting situation and one that can’t be predicted, only observed by trial and error.”
Your colleague needs to check up on the meaning of inlining in C++.
There are two meanings of the word, and it’s important to keep them separated:
According to the C++ standard, a function is
inlineif it is marked with theinlinekeyword, or defined inside the class definition.The only required effect of this, is to disable the One Definition Rule — that is, to make it legal for the definition to be seen in multiple translation units without producing a linker error. Basically, it allows you to put the full definition in a header file
Then there is the “inline” optimization, which consists of taking the function body, and inserting it instead of a function call.
These meanings are almost entirely orthogonal. A function can be inlined by the compiler whether or not you, the programmer, marked it as
inline. (Although it is harder and less common for a compiler to be able to inline if the function is called in a different translation unit than the one in which it was defined) A function markedinlinein C++ may or may not be inlined by the compiler. The compiler tries to estimate the possible benefit of this, based on code size, how frequently the function is called, number of call sites and such heuristics. The result is that the compiler is pretty good at determining when the inlining optimization is worthwhile, and your best bet is usually to leave it to do its thing alone.You should simply mark functions as inline when 1) it is convenient for you, and 2) you want to ensure that the compiler has the option of applying the inlining optimization.
But you’re not forcing the compiler to inline anything. You’re merely arranging the code so that it can, if it chooses to do so, inline the function call.