[GHC] #5623: GHC 7.2.1 Performance Regression: Vector

GHC cvs-ghc at haskell.org
Fri Nov 11 16:49:01 CET 2011


#5623: GHC 7.2.1 Performance Regression: Vector
---------------------------------+------------------------------------------
    Reporter:  dterei            |       Owner:                         
        Type:  bug               |      Status:  new                    
    Priority:  normal            |   Component:  Compiler               
     Version:  7.3               |    Keywords:                         
    Testcase:                    |   Blockedby:                         
          Os:  Unknown/Multiple  |    Blocking:                         
Architecture:  Unknown/Multiple  |     Failure:  Runtime performance bug
---------------------------------+------------------------------------------

Comment(by rl):

 This seems to be due to a rather unfortunate duplication of shared
 computations. Here is a simplified example:

 {{{
 module Foo where

 import qualified Data.Vector.Storable as V
 import Foreign
 import Control.Monad

 data Vec2 = Vec2 {-# UNPACK #-} !Float
                  {-# UNPACK #-} !Float

 instance Storable Vec2 where
   sizeOf _ = sizeOf (undefined :: Float) * 2
   alignment _ = alignment (undefined :: Float)

   {-# INLINE peek #-}
   peek p = liftM2 Vec2 (peekElemOff q 0) (peekElemOff q 1)
     where
       q = castPtr p

   {-# INLINE poke #-}
   poke p (Vec2 a b) = pokeElemOff q 0 a >> pokeElemOff q 1 b
     where
       q = castPtr p


 vsum :: Vec2 -> Float
 {-# INLINE vsum #-}
 vsum (Vec2 a b) = a+b

 xsum :: V.Vector Vec2 -> Float
 xsum = V.sum . V.map vsum
 }}}

 Here is the code that 7.0.4 generates for `xsum`:

 {{{
 $s$wfoldlM'_loop_s18Q :: GHC.Prim.Int# -> GHC.Prim.Float# ->
 GHC.Prim.Float#
 $s$wfoldlM'_loop_s18Q =
   \ (sc_s18K :: GHC.Prim.Int#) (sc1_s18L :: GHC.Prim.Float#) ->
     case GHC.Prim.>=# sc_s18K ww_s187 of _ {
       GHC.Bool.False ->
         let {
           a2_s16J [Dmd=Just L] :: GHC.Prim.Addr#
           a2_s16J = GHC.Prim.plusAddr# ww1_s188 (GHC.Prim.*# sc_s18K 8) }
 in
         case GHC.Prim.readFloatOffAddr# @ GHC.Prim.RealWorld a2_s16J 0
 GHC.Prim.realWorld#
         of _ { (# s2_aOP, x_aOQ #) ->
         case GHC.Prim.readFloatOffAddr# @ GHC.Prim.RealWorld a2_s16J 1
 s2_aOP
         of _ { (# s1_XPG, x1_XPI #) ->
         case GHC.Prim.touch# @ GHC.ForeignPtr.ForeignPtrContents ww2_s189
 s1_XPG
         of _ { __DEFAULT ->
         $s$wfoldlM'_loop_s18Q
           (GHC.Prim.+# sc_s18K 1)
           (GHC.Prim.plusFloat# sc1_s18L (GHC.Prim.plusFloat# x_aOQ
 x1_XPI))
         }
         }
         };
       GHC.Bool.True -> sc1_s18L
     };
 }}}

 And this is with the HEAD:

 {{{
 $s$wfoldlM'_loop_sZQ :: GHC.Prim.Int# -> GHC.Prim.Float# ->
 GHC.Prim.Float#
 $s$wfoldlM'_loop_sZQ =
   \ (sc_sZK :: GHC.Prim.Int#) (sc1_sZL :: GHC.Prim.Float#) ->
     case GHC.Prim.>=# sc_sZK ww_sZq of _ {
       GHC.Types.False ->
         let {
           a2_sXN [Dmd=Just L] :: GHC.Prim.Int#
           a2_sXN = GHC.Prim.*# sc_sZK 8 } in
         case GHC.Prim.readFloatOffAddr#
                @ GHC.Prim.RealWorld
                (GHC.Prim.plusAddr# ww1_sZr a2_sXN)
                0
                GHC.Prim.realWorld#
         of _ { (# s2_aLR, x_aLS #) ->
         case GHC.Prim.readFloatOffAddr#
                @ GHC.Prim.RealWorld
                (GHC.Prim.plusAddr# ww1_sZr a2_sXN)
                1
                s2_aLR
         of _ { (# s1_XNk, x1_XNm #) ->
         case GHC.Prim.touch# @ GHC.ForeignPtr.ForeignPtrContents ww2_sZs
 s1_XNk
         of _ { __DEFAULT ->
         $s$wfoldlM'_loop_sZQ
           (GHC.Prim.+# sc_sZK 1)
           (GHC.Prim.plusFloat# sc1_sZL (GHC.Prim.plusFloat# x_aLS x1_XNm))
         }
         }
         };
       GHC.Types.True -> sc1_sZL
     };
 }}}

 Note how the HEAD duplicates the `plusAddr#` computation. This happens
 several times in the original example and is probably a bug in the
 simplifier.

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5623#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler



More information about the Glasgow-haskell-bugs mailing list