Essentially I want to know what's c++ optimization (/O) & Whole program optimization (/GL) is all about.
Would appreciate deep explanation.
Thanks
Essentially I want to know what's c++ optimization (/O) & Whole program optimization (/GL) is all about.
Would appreciate deep explanation.
Thanks
Whole-program optimization is one aspect of cross-module optimization. This is also called link-time optimization in the context of C and C++, due to those languages' compilation models.
Basically, imagine that the entire source code of your program was pasted into one single file, source and header and library files, everything. In that hyptothetical case, the compiler would have a lot of additional opportunities for optimization: inlining, dead-code removal, deduplication, etc.
Cross-module optimization refers to any technique that attempts to allow such optimizations even within the traditional one-TU-at-a-time compilation that is customary to C and C++ compilers. Typically, this involves adding extra information (possibly even the entire parsed source content) into the individual object files and allowing the linker to perform certain compilation tasks once it sees all the ingredient object files for an application.
MSVC is a so-called Optimizing Compiler. OCs take the code that you have written and re-write parts of it in order to minimize the memory footprint, maximize execution speed, or both. They do this by leveraging deeply technical knowledge of the platform that the code is to be run on, generally targeting specific instruction set.
OCs such as MSVC, GCC, LLVM and many others all utilize many different techniques to accomplish this. The techniques themselves are way beyond the scope of what can be explained in an internet post, even if I knew them all (which I don't). But there are some things that you should keep in mind.
A program that has been optimized is much more difficult to debug than one that has not been. A lot of code might have been moved in terms of both order of execution and locality within the program, and symbols are stripped away.
In general, the Standard allows a compiler to make any change to your program it wishes, so long as the observable behavior of your program is the same "AS-IF" no changes had been made.
The parts of the compiler that are responsible for optimizing your code have been designed and written by dedicated teams of very smart people over many years. The end result is a compiler that, by and large, is far better at optimizing your code than you could ever hope to be. As a rule, it is pointless to try to implement your own micro-optimizations for two reasons. One, the compiler can generally do a better job, and two, micro-optimizations you write will confound the compiler in its ability to implement its own optimizations. By micro-optimizing your code by hand, you might actually make your program perform worse.
Whole Program Optimization is called LTCG in Visual Studio - Link Time Code Generation - here is a writeup on LTCG.