Why prefer /Ob1 over /Ob2 in Visual C++ 9 when there're no tight limitations on executable size?
Visual C++ features /Ob
compiler option that controls function inlining. With /Ob1
only functions marked inline
, __inline
or defined within the class declaration are inlined, while with /Ob2
all fun开发者_高级运维ctions the compiler considers suitable are inlined.
I can imagine some project that has very tight limitations on the image size using /Ob1
instead of /Ob2
. Surprisingly we found a project that has no tight limitations on image size yet it was using /Ob1
and we can't find any reasons why it does so.
Why would a project that has no tight limitations on executable size prefer /Ob1
over /Ob2
?
Because more inlining leads to larger code, which leads to less efficient cache utilization. Since modern CPU:s do aggressive branch-prediction, jumps into/out of a function need not be very costly.
The cache is of limited size though, so by inlining code that forces the CPU to ditch other things that might have been in the cache, thus increasing the number of misses and thereby stalls the CPU has.
The choice of /Ob1
may not be a choice at all, but an oversight. A major cause of seeing the flag /Ob1
nowadays is because it is CMake's default for RelWithDebInfo builds. This can cause major performance differences between RelWithDebInfo and Release builds. I think this is a trap in CMake and RelWithDebInfo should be changed to use /Ob2
by default.
There is no real reason, in terms of speed. Inlining has certain tradeoffs, but the compiler's heuristic is almost certainly smarter about it than a user.
I've encountered a compiler bug with /Ob2 with 64-bit Release builds. Using /Ob1 makes the problem disappear. The developer of the project may have encountered the same or a similar problem.
- Compile times may be slightly faster in the first case.
- The compiler may not always make the best decisions on which functions to inline. Inlining more to make single functions faster, may in total make the entire program run slower, due to side effects like cache spill.
精彩评论