Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#10102 closed bug (duplicate)

GHC inlines past lambda in do-notation

Reported by: nomeata Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.8.4
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:



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)

Attachments (1)

Test_5_input.txt.gz (56.0 KB) - added by nomeata 5 years ago.

Download all attachments as: .zip

Change History (4)

Changed 5 years ago by nomeata

Attachment: Test_5_input.txt.gz added

comment:1 Changed 5 years ago by nomeata

BTW, it does not occur with -fno-state-hack. So it’s likely a duplicate of the known problem that the state hack can go wrong, e.g. #1168, #9349. Might be fixed by #9388.

comment:2 Changed 5 years ago by nomeata

Resolution: duplicate
Status: newclosed

comment:3 Changed 5 years ago by Simon Peyton Jones <simonpj@…>

In ee56dc56a4a0f556894c4d2bd04c3d4ca73e95a1/ghc:

Tidy up and improve comments about one-shot info

(Triggered by investigating Trac #10102 etc.)
Note: See TracTickets for help on using tickets.