[vector] #20: Optimization strategy unclear (-Odph / -O2)

vector vector at projects.haskell.org
Thu May 20 06:46:05 EDT 2010

#20: Optimization strategy unclear (-Odph / -O2)
Reporter:  choener                      |        Owner:     
    Type:  defect                       |       Status:  new
Priority:  major                        |    Milestone:  0.7
 Version:  0.6                          |   Resolution:     
Keywords:  documentation, optimization  |  
Comment (by rl):

 Gosh, -Odph is really slow here. That can be fixed by manually eta-
 expanding the functions. Change

 mean = fini . U.foldl go (T 0 0)


 mean xs = fini (U.foldl go (T 0 0) xs)

 This is because -Odph turns on -finline-if-enough-args which prevents
 functions from being inlined if they aren't applied to enough arguments.
 This, in turn, leads (.) to not being inlined in this case which is rather
 bad. I'm not sure why GHC doesn't eta-expand automatically here. As usual,
 this works fine in 6.13.

 You are quite right that no fusion will happen if the functions don't get
 inlined and that it is thus desirable to give those functions an INLINE
 pragma. With 6.12, you then have to make sure that GHC really does inline
 them everywhere they are used. This is a bit hard to achieve sometimes, as
 evidenced by this discussion. Unfortunately, there is nothing the library
 can do about it - it's a compiler problem.

 In this particular example, there is a way to keep the functions as they
 are in your original program, including INLINE pragmas, by simply changing

 main = defaultMain [ bench "mean 1e5" $ nf mean  vec1e5
                    , bench "mean'1e5" $ nf mean' vec1e5


 main = defaultMain [ bench "mean 1e5" $ nf (\xs -> mean  xs) vec1e5
                    , bench "mean'1e5" $ nf (\xs -> mean' xs) vec1e5

 Now both functions do get inlined and everything works as expected, both
 with -O2 and with -Odph. I should have thought of this earlier.

Ticket URL: <http://trac.haskell.org/vector/ticket/20#comment:8>
vector <http://trac.haskell.org/vector>
Package vector

More information about the vector mailing list