Or are there some optimizations that can only be done at compile time (and therefore only work within compilation units)? I ask because in C, the compilation unit is a source file, and I'm trying to understand if there is any reason not to split source code into separate files in some circumstances (e.g. an optimization that could have been done if all the source were in one file was not done).
-
2Wow - all code in one while - I'd hate to be the guy who follows you who has to maintain this.. – Nim Feb 24 '15 at 14:50
-
How would link-time optimization handle code-pruning within a function? Maybe I don't understand the question... – Scott Hunter Feb 24 '15 at 14:52
-
1Yes, LTO was introduced to enable all optimizations without having to compile everything in one go from sources (irrespective of multiple TU at once or all one TU). – Deduplicator Feb 24 '15 at 14:52
-
@Nim, all code in one **file** :) – David Ranieri Feb 24 '15 at 14:52
-
If there were any advantage to putting everything into a single translation unit, it would have been fixed by compiler designers out of fear that programmers would start exploiting it. – Sergey Kalinichenko Feb 24 '15 at 14:52
-
I'm not talking about all source code for the program. I'm just wondering if there are any circumstances where splitting some code into multiple files can cause a performance degradation. This is really just a curiosity of mine. – awelkie Feb 24 '15 at 14:52
-
3@DurnWhippersnapper As with many programming-related questions of the form _Are there any circumstances where_ **doing X** _will cause a performance degradation?_, the answer is probably _literally, yes; practically, no._ There will always be pathological cases that you can probably cook up, but that doesn't mean you should worry about them. – MooseBoys Feb 24 '15 at 15:00
-
@AlterMann, Äbä, genau.. ;) – Nim Feb 24 '15 at 15:02
2 Answers
A typical (simplified) compile might look like
1) Pre-process
2) Parse code to internal representation
3) Optimize code
4) Emit assembly language
5) Assemble to .o file
6) Link .o file to a.out
LTOs are typically achieved by dumping the internal compiler representation to disk between steps 2 and 3, then during the final link (step 6) going back and performing steps 3-5. This could be depending on the compiler and version however. If it follows this pattern then you would see LTO equivalent to Compile Time optimizations.
However ...
Having very large source files can be annoying -- Emacs starts to choke on source files >10MB.
If you are in a multi-user development environment, depending on your SCM you may have a lot of trouble if multiple engineers are working on the same file.
If you use a distributed build system you perform compiles in parallel. So if it takes 1 second each to compile and optimize a file, and you have 1000 files and 1000 build agents, your total compile time is 1 second. If you are doing all your optimization for all 1000 files during the final you will have 999 agents sitting idle and 1 agent spend an eternity doing all your optimization.

- 13,845
- 6
- 50
- 57
academic example:
main()
{
int i;
for (i = 0; i < MAX; i++) {
fun(i);
}
}
fun(int i)
{
if (i == 0) {
doSomething();
}
}
if fun
is in the same compilation unit, and data-flow-analys is enabled, the foor-loop could be optimized to a single function call.
BUT: I would stay with MooseBoys' comment.

- 5,984
- 2
- 38
- 55