-O vs. -O2

Roman Leshchinskiy rl at cse.unsw.edu.au
Sun May 9 22:43:49 EDT 2010


On 09/05/2010, at 07:50, Duncan Coutts wrote:

> On Wed, 2010-05-05 at 21:24 +1000, Roman Leshchinskiy wrote:
>> Whenever I do cabal sdist on one of my projects, I get this warning:
>> 
>> Distribution quality warnings:
>> 'ghc-options: -O2' is rarely needed. Check that it is giving a real benefit
>> and not just imposing longer compile times on your users.
>> 
>> This finally got me curious and I did a nofib run to compare -O to
>> -O2. The results are below (this is with the current HEAD).
>> 
>> Is there a real-world example of -O2 causing significantly longer
>> compile times without providing a real benefit? If not, would it
>> perhaps make sense for Cabal to use -O2 by default or even for GHC to
>> make the two flags equivalent?
> 
> It should be -O1 for default/balanced optimisations and -O2 for things
> involving a bigger tradeoff in terms of code size or compile time. so
> any optimisations in -O2 that GHC HQ believe are a no-brainer for the
> majority of packages should be moved into -O1.

Unless I'm mistaken, the only difference between -O1 and -O2 are SpecConstr and LiberateCase. These are quite heavily constrained by default (e.g., SpecConstr will not specialise big functions and will not generate more than 3 specialisations for smaller ones).

> It's fine for people writing performance sensitive code to use -O2 in
> their packages. It's just not something we need to encourage for random
> packages. Before we added that warning, many package authors were not
> really thinking and just chucking in -O2 "because 2 is bigger than 1 so
> it must be better right?". There certainly used to be packages that took
> longer to compile, generated more code, and ran slower when using -O2.
> That was some time ago of course.

And yet that doesn't really happen for nofib programs. So if there are still examples of this, we should include them in nofib. Quite probably, they're just running into bugs in the simplifier which should be fixed.

Also, the Cabal warning only talks about compile times. If it's just between compile times and better performance, then the latter should be the default, IMO, since you only install things once and run them many times.

Roman




More information about the Glasgow-haskell-users mailing list