#1957: Program that runs slower with optimizations on
----------------------+-----------------------------------------------------
 Reporter:  clanehin  |          Owner:         
     Type:  bug       |         Status:  new    
 Priority:  normal    |      Milestone:         
Component:  Compiler  |        Version:  6.8.1  
 Severity:  normal    |     Resolution:         
 Keywords:            |     Difficulty:  Unknown
 Testcase:            |   Architecture:  x86    
       Os:  Linux     |  
----------------------+-----------------------------------------------------
Comment (by dons):

 Looks like the printFib is messing with the inlining:

 Across 2 modules, -O2 and -O are 5x slower than -Onot.
 In a single module, -O2 wins easily.

 Something fishy going on.

 Looking at the unfolding in the .hi file for NaiveFib.hs:

 {{{
 interface main:NaiveFib 3 608120071117 where
 export main:NaiveFib naiveFib printFib
 module dependencies:
 package dependencies: base
 orphans: base:GHC.Base
 family instance modules:
 3 printFib :: GHC.Num.Integer -> GHC.IOBase.IO ()
     {- Arity: 2 Strictness: LL
        Unfolding: (NaiveFib.a
                      `cast`
                    (GHC.Num.Integer -> sym ((GHC.IOBase.:CoIO) ()))) -}
 3 naiveFib :: GHC.Num.Integer -> GHC.Num.Integer
     {- Arity: 1 HasNoCafRefs Strictness: S -}
 3 a :: GHC.Num.Integer
        -> GHC.Prim.State# GHC.Prim.RealWorld
        -> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)
     {- Arity: 2 Strictness: LL
        Unfolding: (\ x :: GHC.Num.Integer eta :: GHC.Prim.State#
 GHC.Prim.RealWorld ->
                    case @ (# GHC.Prim.State# GHC.Prim.RealWorld, () #)
 GHC.IO.a24
 GHC.Handle.stdout
 (GHC.Num.$wshowsPrec
 0
 (NaiveFib.naiveFib x)
 (GHC.Base.[] @ GHC.Base.Char))
 eta of wild { (# new_s, a59 #) ->
                    GHC.IO.$wa9 GHC.Handle.stdout '\n' new_s }) -}
 vectorised variables:
 vectorised tycons:
 vectorised reused tycons:
 }}}

 Looks like the print is mucking up the optimiser a bit?
 Let's try moving the printFib line into Main.hs:

 {{{
 $ time ./Main-O2
 24157817
 24157817
 24157817
 24157817
 24157817
 24157817
 24157817
 24157817
 24157817
 24157817
 ./Main-O2  2.99s user 0.01s system 99% cpu 3.028 total
 }}}

 And, its fixed if we inline printFib too:

 {{{
  printFib = print . naiveFib
  {-# INLINE printFib #-}
 }}}

 Simon, what's going on with the optimiser here?

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/1957#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
Glasgow-haskell-bugs@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to