Is connection time optimization in gcc 5.1 good enough to refuse to create simple functions?

Out of habit, I often write inline function definitions for simple functions like this one (contrived example)

class PositiveInteger { private: long long unsigned m_i; public: PositiveInteger (int i); }; inline PositiveInteger :: PositiveInteger (int i) : m_i (i) { if (i < 0) throw "oops"; } 

I usually like to separate interface files and implementation files, but, nevertheless, this is my habit of those functions that the voice in my head speaks about, it will probably suffer greatly in hot spots.

I know that advice is โ€œprofile one,โ€ and I agree, but I could have avoided the whole burden of profiling if I knew a priori that the compiler would produce the same final object code, whether functions like this were included at the time of compilation or link, (In addition, I believe that the implemented profiling code itself can lead to a time change that hides the effect of very simple functions, such as above.)

GCC 5.1 has just released an LTO ad (link time optimization). How good are they really? What functions can I safely disable, knowing the final executable file, will not be affected?

+5
source share
2 answers

You have already answered your question: if you are not aiming at an embedded system of any type with limited resources, first write the code for clarity and ease of maintenance. Then, if performance is unacceptable, you can profile and focus your efforts on actual hot spots. Think about it: if you write clearer code that takes an extra 250 ns that are not noticeable in your use case, the extra time doesn't matter.

+1
source

GCC with LTO does cross-module insertion, so most of the time you should not see the difference in code quality. Offline function declarations are also not duplicated in translation units and compile faster / produce smaller object files.

GCC-elliptic algorithms, however, consider the inline keyword to be the key as a hint that the function is probably good for being built-in and increasing the size limits of the function. Likewise, a little extra hint of functions declared in the same translation unit as the called one is required. For small functions like the one in your example, this should not make any difference.

0
source

Source: https://habr.com/ru/post/989902/


All Articles