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

GHC ghc-devs at haskell.org
Mon Sep 19 02:37:33 UTC 2016


#8763: forM_ [1..N] does not get fused (10 times slower than go function)
-------------------------------------+-------------------------------------
        Reporter:  nh2               |                Owner:
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:  8.2.1
       Component:  Compiler          |              Version:  7.6.3
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
 Type of failure:  Runtime           |  Unknown/Multiple
  performance bug                    |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Description changed by bgamari:

@@ -6,1 +6,1 @@
- {{{
+ {{{#!hs
@@ -16,3 +16,3 @@
- where
- go !n | n == bex = return ()
- | otherwise = f n >> go (n+1)
+   where
+     go !n | n == bex = return ()
+           | otherwise = f n >> go (n+1)

New description:

 Apparently idiomatic code like {{{forM_ [1.._N]}}} does not get fused
 away.

 This can give serious performance problems when unnoticed.

 {{{#!hs
 -- 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#comment:28>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list