Re: -Os versus -O2



On Mon, Jun 25, 2007 at 09:08:23AM +0200, Segher Boessenkool wrote:
In my experience, -Os produced faster code on gcc-2.95 than -O2 or -O3.

On what CPU? The effect of different optimisations varies
hugely between different CPUs (and architectures).

x86

It was not only because of cache considerations, but because gcc used
different tricks to avoid poor optimizations, and at the end, the CPU
ended executing the alternative code faster.

-Os is "as fast as you can without bloating the code size",
so that is the expected result for CPUs that don't need
special hand-holding around certain performance pitfalls.

With gcc-3.3, -Os show roughly the same performance as -O2 for me on
various programs. However, with gcc-3.4, I noticed a slow down with
-Os. And with gcc-4, using -Os optimizes only for size, even if the
output code is slow as hell. I've had programs whose speed dropped
by 70% using -Os on gcc-4.

Well you better report those! <http://gcc.gnu.org/bugzilla>

No, -Os is for size only :

-Os Optimize for size. -Os enables all -O2 optimizations
that do not typically increase code size. It also
performs further optimizations designed to reduce code
size.

So it is expected that speed can be reduced using -Os. I won't report
a thing which is already documented !

But in some situtations, it's desirable to have the smallest possible
kernel whatever its performance. This goes for installation CDs for
instance.

There are much better ways to achieve that.

Optimizing is not a matter of choosing *one* way, but cumulating
everything you have. For instance, on a smart boot loader, I have
a kernel which is about 300 kB, or 700 kB with the initramfs. Among
the tricks I used :
- -Os
- -march=i386
- align everything to 0
- replace gzip with p7zip

Even if each of them reduces overall size by 5%, the net result is
0.95^4 = 0.81 = 19% gain, for the same set of features. This is
something to consider.

Regards,
Willy

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • Re: [BUG] x86 kenel wont boot under Virtual PC
    ... Instead of just including optimizations for the selected ... The kernel will not necessarily run on earlier architectures ... I thought you agreed that CPU virtualization can be a problem? ... exactly how does the kernel fail to boot with latest -git? ...
    (Linux-Kernel)
  • Re: Would a locally compiled kernel be faster?
    ... binary executable code better than a kernel that comes with a distro? ... need to specify machine specific optimizations for your specific CPU. ...
    (comp.os.linux.misc)
  • Re: [BUG] x86 kenel wont boot under Virtual PC
    ... optimizations have historically really been about _optimizing_, ... those exceptions have been way more imporant than NOPL). ... actually enables EMBEDDED and does all his choices by hand should no ... The difference between a kernel compiled for generic and once compiled for a specific CPU can be very significant. ...
    (Linux-Kernel)
  • Re: Suggestions for Core 2 Duo systems that use PCI - not PCI express?
    ... just maxing out the board with a faster socket 478 CPU isn't such ... VirtuaDub USENET group or maybe a web forum. ... OS-specific optimizations dynamically dispatched. ... for UI, reading from disk, processing, and writing to disk. ...
    (alt.comp.hardware.pc-homebuilt)
  • Re: [PATCH] 2.6 workaround for Athlon/Opteron prefetch errata
    ... "Select the CPU the kernel will be run on." ... you can pick one CPU. ... there is an "Advanced processor selection". ... Selecting this option disables some of the optimizations for the primary ...
    (Linux-Kernel)