Answered by:
Bad code optimization of x32 apps with VC 2008 Express

Question
-
I have a VC Win32 project, created originally with MS VS 2005 (compiled both for x32 and x64).
I migrated this project to VC 2008 Express with Windows 7 SDK added in order to be able to
compile x64 versions of applications. The Release versions of this application were optimised for speed. The same optimization options (/O2, /Oi, /Ot, /GL) were used, of course, in migrated project.
But, whereas x64 compilations (performed, clearly, by SDK's version of CL.exe) were with approximately the same size as the corresponding versions compiled with VS 2005, the "new" x32
version is about 100K bigger than the "old" version (500K instead of 400K). Well, I had not yet
tested and compared speeds of both version (this is the most important parameter of the prog),
but this big difference in size looks strange to me.
Having in mind that "core" optimization (I do not know what exactly does this mean) of Express
version is the same as that of Standard and above versions of VS 2008, as MS guys say.
And the difference in optimization levels lies in some "Profile guide optimization" (again I
do not know the meaning of this phrase).
Of course, x32 bit versions are not that important now as x64 are, but there remains yet
some class of users, preferring to work with the ancient x32 versions of Windows.
So can somebody explain, whether it is possible to optimize x32 Win32 apps in VC 2008 Express
at least to the degree comparable with that of good old VC 2005 (which, unfortunately, is unable
to understant some instructions available in Intel i7 processors (like popcnt(x)).Wednesday, March 3, 2010 7:27 PM
Answers
-
> Small doesn't always mean fast. So if the x86 code generator produced larger code in VC2008 it is
> also just as possible that the 2008 generator is producing code which is larger but faster than the
> 2005 equivalent.
In fact this code turned out to be not optimized at all! The reason for this is that
due to some bug in the migration tool or in IDE itself, the option /O2 of compiler
was not swithed on (for x32 platform only!),
though IDE showed just this option for both x32 and x64 platforms.
I discovered this looking on compiler's command line options. When I added the option /O2 manually
everything became OK. The other way is to choose in IDE "Custom", apply it,
then to rechoose "Maximise speed (/O2)" in compiler optimization settings.
By the way, originally the text "Maximize speed (/O2)" of this option was printed in normal font,
but after I have rechosen it manually it became bold faced. So the chosen options which
are not bold face, only create the illusion that they are really set.
> But to give you the capabilities of the compiler given with the express version. Basically it is identical > to the compiler shipped with Standard or higher.
Is it true for VC 2005 as well? I have VS 2005 Standard Edition installed, and GPO seems to be lacking
in this edition as well --- there is no gpodb80.dll in the installation. Though the corresponding
options for profile optimization are present in the IDE.- Marked as answer by Vlad0 Thursday, March 4, 2010 6:18 PM
Thursday, March 4, 2010 2:56 PM
All replies
-
Small doesn't always mean fast. So if the x86 code generator produced larger code in VC2008 it is also just as possible that the 2008 generator is producing code which is larger but faster than the 2005 equivalent.
Space vs speed is everywhere. In algorithms like sorting, the sorts which take up space often are faster than the sorts which take up less space. The same is true with processor instructions. For example, loop unrolling is often a technique used to increase the speed of loops by unrolling the loop if it is safe. So instead of the loop executing branch instructions more, it will put several iterations of the loop one after another. So you should really compare the execution times before you come to the conclusion that the VS2008 optimisation is bad. There have also been changes to the tuning of the processor used in 2008. So the extra space could be from this.
But to give you the capabilities of the compiler given with the express version. Basically it is identical to the compiler shipped with Standard or higher. The only differences is down to what is shipped with it. Express has the minimum amount, so no openmp support, no profile guided support etc. but the core compiler is the same. So a program compiled with /O2 will produce the same code in Express as it would in the higher versions. Whats more, since you said you have installed the WinSDK then you will have the same compiler suite which ships with TS.
For profile guided optimisations, if you built with this enabled you really would know about it. To get profile guided optimisations yo work properly you have to build the application at least twice. Then between the first and second build you have to use the application lots of times to train it. The second build will relink the program with information gained from running it these times to figure out the best way to optimise and lay out functions in the resultant image. So pogo wouln't affect the size of the program too much.
Visit my (not very good) blog at http://c2kblog.blogspot.com/Thursday, March 4, 2010 1:39 AM -
> Small doesn't always mean fast. So if the x86 code generator produced larger code in VC2008 it is
> also just as possible that the 2008 generator is producing code which is larger but faster than the
> 2005 equivalent.
In fact this code turned out to be not optimized at all! The reason for this is that
due to some bug in the migration tool or in IDE itself, the option /O2 of compiler
was not swithed on (for x32 platform only!),
though IDE showed just this option for both x32 and x64 platforms.
I discovered this looking on compiler's command line options. When I added the option /O2 manually
everything became OK. The other way is to choose in IDE "Custom", apply it,
then to rechoose "Maximise speed (/O2)" in compiler optimization settings.
By the way, originally the text "Maximize speed (/O2)" of this option was printed in normal font,
but after I have rechosen it manually it became bold faced. So the chosen options which
are not bold face, only create the illusion that they are really set.
> But to give you the capabilities of the compiler given with the express version. Basically it is identical > to the compiler shipped with Standard or higher.
Is it true for VC 2005 as well? I have VS 2005 Standard Edition installed, and GPO seems to be lacking
in this edition as well --- there is no gpodb80.dll in the installation. Though the corresponding
options for profile optimization are present in the IDE.- Marked as answer by Vlad0 Thursday, March 4, 2010 6:18 PM
Thursday, March 4, 2010 2:56 PM -
Profile guided optimisations are missing from standard or below, but this isn't part of the compiler itself. (This is what I assume you mean by GPO.)
Whats more, if you notice what I said, I said that since you installed the Windows SDK then you will have the same tools as in Team System. This is true of the Windows SDK from the 2005 period too. So if you look for and get the older Windows Vista SDKs then you will get the VC 2005 SP1 tools which came with 2005 TS.
But if you were to look here you would know what is available for each version of VS.
Since you used the project upgrade wizard, the fact that /O2 wasn't switched on also isn't a surprise. If you had said that you used the IDE's tool then it would have been easier to figure out what the problem was. You see, the upgrade wizards are always problematic to the point that it is recommended that you just recreate the project if you see any problems.
"By the way, originally the text "Maximize speed (/O2)" of this option was printed in normal font,
but after I have rechosen it manually it became bold faced. So the chosen options which
are not bold face, only create the illusion that they are really set."
This is not true at all, all this means is that it is set to the default for the configuration. The bold face font is there to depict that it has been changed. The only way for sure to be certain that it is enabled in a project is by checking out the command line. Although you are right in this case that /O2 should always be bold since it isn't the default setting. So I really would suggest you actually figure these things out before you make any kind of assumptions.
Visit my (not very good) blog at http://c2kblog.blogspot.com/Thursday, March 4, 2010 7:11 PM -
> Profile guided optimisations are missing from standard or below,
> but this isn't part of the compiler itself.
Sorry, I was not too attentive, so failed to notice that You were
meaning by "compiler" CL.exe literally, whereas I
comprehended this in a much wider sense, which included
various additional tools, including profiler.
> This is what I assume you mean by GPO
Sorry again -- it was a typo. Of course, I meant PGO.
> if you were to look here you would know what is available for each version of VS.
Thanks for the link! I've looked through this page and have one question.
It is stated there that openMP is not supported neither in Express nor in Standard
edition. But in my code I use some procedures declared in omp.h
and the code compiles OK (even without /openmp compiler option,
which is necessary due to MSDN). Does this mean that the openMP
support was added to VC express by Windows 7 SDK?
> Since you used the project upgrade wizard, the fact that /O2 wasn't switched on
> also isn't a surprise. If you had said that you used the IDE's tool then it
> would have been easier to figure out what the problem was.
Well, in fact I do not know the difference between "project upgrade wizard"
and "IDE's tool", so can not tell which of these 2 I have used exactly.
What I really did was:
1) I have opened VC 2005 .sln file from inside VC 2008 Express IDE;
2) IDE said that I have to convert this solution (and .vcproj)
to the new format;
3) I said OK, and IDE has converted both solution and projects,
saying that the convertion was OK, with no problems.
There were no problems really. In the sense that the compilation
for both win32 and x64 platforms went OK.
But, comparing sizes of executables compiled with VC 8 and
the corresponding executables compiled with VC 9 I saw
that Win32 executable of VC 9 was more than 25% bigger
than that of VC 8. Comparing project properties of both projects
I saw that the parameter C/C++-->Optimization-->Optimization
was set to "Maximize speed (/O2)" in both projects. The difference
of fonts in texts of this parameter I've noted only later.
After I've seen that the compiler command line option lacks
in VC 9 project.
The difference in project options was present for Win32 platform only.
Everything was OK with x64 platform.
> You see, the upgrade wizards are always problematic to the point that it is
> recommended that you just recreate the project if you see any problems.
For this particular project (with about 70 .c and .h files) it was much easier
just to change one property of already existing project. The real problem here
was to discover the bug created during migration.
>> So the chosen options which are not bold face, only create the illusion
>> that they are really set.
>
> This is not true at all
Well, may be my generalization is not correct. But in my particular
case it was true. To be precise, I have not chosen the option
"Maximize speed (/O2)" manually (or, rather, "mousely").
It was already set by migration tool in the "Optimize" dropbox.Friday, March 5, 2010 6:07 PM -
It seems that you didn't catch the significance of me saying that when you install the Windows SDK you will end up with the same compiler toolset as Team System. Since openmp is part of the Team System toolset then openmp working in this case isn't surprising.
>Well, in fact I do not know the difference between "project upgrade wizard"
>and "IDE's tool", so can not tell which of these 2 I have used exactly.
They are referring to the same thing, the project uprgrade wizard is the IDE's tool to convert an older VC project into a newer VC project. But coming out of it with only a setting not actually being set is pretty good. I've had builds being completely broken because of the project upgrade wizard.
>For this particular project (with about 70 .c and .h files) it was much easier
>just to change one property of already existing project. The real problem here
>was to discover the bug created during migration.
Yes in this case just changing one setting is prefereable. I must apologise though, I meant to say "any major problems" as one setting which can be easily fixed can't be called major. But in a case where no matter what you do, even after setting and resetting settings the project will not compile, then this is the case when you just recreate the projct.
Visit my (not very good) blog at http://c2kblog.blogspot.com/Saturday, March 6, 2010 3:09 AM -
> It seems that you didn't catch the significance of me saying that > when you install the Windows SDK you will end up with the same > compiler toolset as Team System. Since openmp is part of the
> Team System toolset then openmp working in this case isn't
> surprising.
Yes, I really didn't. Just because I did not (and do not yet)
know which things exactly belong to this "compiler toolset"
and which do not :).
For example, I thought earlier that profiler
is a member of this toolset. Now I know that
this tool is not a member.Saturday, March 6, 2010 3:13 PM