[GHC] #8763: forM_ [1..N] does not get fused (10 times slower than go function)

GHC ghc-devs at haskell.org
Sun Feb 9 22:04:54 UTC 2014


#8763: forM_ [1..N] does not get fused (10 times slower than go function)
------------------------------+--------------------------------------------
       Reporter:  nh2         |             Owner:
           Type:  bug         |            Status:  new
       Priority:  normal      |         Milestone:
      Component:  Compiler    |           Version:  7.6.3
       Keywords:              |  Operating System:  Unknown/Multiple
   Architecture:              |   Type of failure:  Runtime performance bug
  Unknown/Multiple            |         Test Case:
     Difficulty:  Unknown     |          Blocking:
     Blocked By:              |
Related Tickets:              |
------------------------------+--------------------------------------------
 Apparently idiomatic code like {{{forM_ [1.._N]}}} does not get fused
 away.

 This can give serious performance problems when unnoticed.

 {{{
 -- Slow:
 forM_ [0.._N-1] $ \i -> do ...

 -- Around 10 times faster:
 loop _N $ \i -> do ...

 {-# INLINE loop #-}
 loop :: (Monad m) => Int -> (Int -> m ()) -> m ()
 loop bex f = go 0
 where
 go !n | n == bex = return ()
 | otherwise = f n >> go (n+1)
 }}}

 Full code example: https://gist.github.com/nh2/8905997 - the relevant
 alternatives are commented.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/8763>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list