Compile code as a single, automatically compiled file to provide better compiler code optimization

Suppose you have a program in C, C ++, or any other language that uses the "compile-objects-then-link-them" scheme.

If your program is small, it can compromise several files to simplify code management (and reduce compilation time). Also, after a certain degree of abstraction, you probably have a deep hierarchy of calls. Especially at the lowest level, where the tasks are most repeatable, most often you want to impose a common structure.

However, if you fragment your code in different object files and use a very abstract architect for your code, this can lead to increased productivity (which is bad if you or your manager emphasize performance).

One way to get around this can be with extensive inlining - this is the metaprogramme approach of the template: in each translation module, you include all the code for your common, flexible structures and rely on the compiler to counter performance problems. I want to do something like this without templates - say, because they are too difficult to handle, or because you are using simple C.

You can write all your code in one file. That would be terrible. How about writing a script that combines all of your code into a single source file and compiles it? Requiring your source files not to be written too wildly. Then the compiler would probably apply much more optimization (embedding, enameling dead code, compile-time arithmetic, etc.).

Do you have experience or objections to this "trick"?

+3
source share
6 answers

. MSVC, GCC clang (GCC clang " " ), . , , ( , ++) .

, , .

, . .

+8

, iirc KDE , ​​. , - , .

, :

  • - namespace { int x; }; .
  • -, . using namespace foo; .cpp -
  • C- anon, . static int i; cpp .
  • #define .cpp - ,

/ ( ) - , , .

+5

, . , .

+2

, #include C .

, " ", , .

+2
0

, , , ? , ,

  • () ,

  • (, 10% ).

, .

, .

  • - , .

  • - , .

. . , , , . , , , , .

If you gain experience tuning performance like this , you will learn about design approaches that lead to performance issues. What I see again and again is too many levels of abstraction, redundant notification, an overridden data structure, and the like.

0
source

All Articles