Reputation: 2632
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).
Upvotes: 0
Views: 296
Reputation: 14823
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.
Upvotes: 1
Reputation: 6070
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.
Upvotes: 0