[GHC] #10102: GHC inlines past lambda in do-notation

GHC ghc-devs at haskell.org
Fri Feb 20 18:39:46 UTC 2015


#10102: GHC inlines past lambda in do-notation
-------------------------------------+-------------------------------------
              Reporter:  nomeata     |             Owner:
                  Type:  bug         |            Status:  new
              Priority:  normal      |         Milestone:
             Component:  Compiler    |           Version:  7.8.4
              Keywords:              |  Operating System:  Unknown/Multiple
          Architecture:              |   Type of failure:  None/Unknown
  Unknown/Multiple                   |        Blocked By:
             Test Case:              |   Related Tickets:
              Blocking:              |
Differential Revisions:              |
-------------------------------------+-------------------------------------
 Hi,

 a friend of mine discovered this weird behaviour:

 The program, when run on the attached file, takes quite some time.
 {{{
 import System.IO
 import Control.Monad
 import qualified Data.Map as M

 main :: IO ()
 main = do
     n <- read `fmap` getLine
     q <- read `fmap` getLine

     mts <- replicateM n $ do
         [a,b] <- words `fmap` getLine
         return (a,b)

     let mtdb2 = M.fromList mts

     --M.null mtdb2 `seq` return ()

     replicateM_ q $ do
         a <- getLine
         print $ M.lookup a mtdb2
     return ()
 }}}

 But if I uncomment the use of `mtdb2` outside the replicate, it runs fast.
 My conclusion is that GHC inlines `mtdb2` into the body of the loop,
 repeating the construction of the map over and over.

 If `forM [1..q] $ \_ -> do` is used instead, this does not happen –
 probably because GHC does _not_ inline past that lambda.

 (I didn’t attempt to understand the core yet)

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


More information about the ghc-tickets mailing list