Do multiple compilation units still stand when (runtime) >>> (compile time)?

Based on my understanding, the main advantages of creating a program with several compilation units are the reuse of components and reduction of compilation time when small changes are included.

I also think (perhaps erroneously) that there is a penalty associated with this, that functions that are defined in their own compilation units cannot be declared as "inline".
[I understand that this keyword does not actually make the compiler work with the built-in extension functions, but I understand that it gives the compiler more flexibility for optimization, and therefore it is worth including it where possible.]

So far so good?

My real question is that cost-benefit analysis still favors several compilation units when a program solves a complex modeling problem, and is required to continuously iterate through its main loop for several months on a cluster to generate useful output.

Let's say that a program for several compilations takes several minutes to compile, when the same program is re-configured as a single compilation unit, it takes several hours to compile ... if a single compilation unit declares all functions as built-in and, therefore, represents more Opportunities for optimization, it seems reasonable to expect that the execution time may decrease by several percent, and not compensate for the additional compilation time.

Are there any good rules for such situations, or is it highly dependent on the situation?

+4
source share
3 answers

As already mentioned by others, the main advantage of decomposing a program into different compilation units is readability. A shorter compilation time is some nice side effect of the idea.

If you are interested in inlining, you can resort to generating the time code and optimizing the connection time . The combination of program decomposition with compilation units and LTO looks like the perfect solution, although it is unclear whether LTO can perform the optimization performed by the compiler with a full definition of the function. For example, I don’t know if LTO supports optimization of return values ​​in C ++, since this is something done at a high level of abstraction. Performance tests are required. ( Edit ): RVO is performed even in the absence of LTO and advanced tricks like these, at least on gcc and clang [I tried]. Most likely, this optimization is performed by changing the ABI of the function, which takes a "hidden pointer" to the object that should be constructed and returned to the function.)

The third solution worth exploring is to use something like sqlite amalgamation β€” the process of putting different compilation units into one giant. c file. Looks like it requires some kind of heavy user infrastructure, though.

+2
source

Some compilers / linkers may automatically perform built-in functions, even if they are defined in one compilation unit and used in another. Microsoft Linker can certainly do this.

For me, the main advantage of dividing code into separate compilation units is the general organization of the code, and I always make decisions based on this fact, not the considerations you have. Also, remember that larger programs are often processed by more than one person at a time. Obviously, separate compilation units are a big advantage.

In short, I think it is highly dependent on the situation. I think your situation is rare.

+2
source

Do not forget the rule 80-20. 80% of your executable programs are spent on 20% of the code. Maybe you can have these 20% in one compilation unit, and the rest are well organized?

And about the organization of sources, you can still put the algorithms in the headers (either as templates or as built-in functions), and then compile the source from them.

0
source

All Articles