开发者

Single Source Code vs Multiple Files + Libraries

开发者 https://www.devze.com 2023-03-11 17:34 出处:网络
How much effect does having multiple files or compiled libraries vs. throwing everything (>10,000 LOC) into one source have on the final binary? For example, instead of linking a Boost library separat

How much effect does having multiple files or compiled libraries vs. throwing everything (>10,000 LOC) into one source have on the final binary? For example, instead of linking a Boost library separat开发者_如何学Pythonely, I paste its code, along with my original source, into one giant file for compilation. And along the same line, instead of feeding several files into gcc, pasting them all together, and giving only that one file.

I'm interested in the optimization differences, instead of problems (horror) that would come with maintaining a single source file of gargantuan proportions.

Granted, there can only be link-time optimization (I may be wrong), but is there a lot of difference between optimization possibilities?


If the compiler can see all source code, it can optimize better if your compiler has some kind of Interprocedural Optimization (IPO) option turned on. IPO differs from other compiler optimization because it analyzes the entire program; other optimizations look at only a single function, or even a single block of code

Here is some interprocedural optimization that can be done, see here for more:

  • Inlining
  • Constant propagation
  • mod/ref analysis
  • Alias analysis
  • Forward substitution
  • Routine key-attribute propagation
  • Partial dead call elimination
  • Symbol table data promotion
  • Dead function elimination
  • Whole program analysis

GCC supports this kind of optimization.

This interprocedural optimization can be used to analyze and optimize the function being called.

If compiler can not see the source code of the library function, it cannot do such optimization.


Note that some modern compilers (clang/LLVM, icc and recently even gcc) now support link-time-optimization (LTO) to minimize the effect of separate compilation. Thus you gain the benefits of separate compilation (maintenance, faster compilation, etc.) and these of whole program analysis.

By the way, it seems like gcc has supported -fwhole-program and --combine since version 4.1. You have to pass all source files together, though.

Finally, since BOOST is mostly header files (templates) that are #included, you cannot gain anything from adding these to your source code.

0

精彩评论

暂无评论...
验证码 换一张
取 消