
#11677: Dramatic de-optimization with "-O", "-O1", "-O2" options -------------------------------------+------------------------------------- Reporter: malphunction | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: | Keywords: optimization | deoptimization Operating System: Linux | Architecture: x86_64 Type of failure: Runtime | (amd64) performance bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by bgamari): Thanks for the excellent description and testcase! The problem here is that GHC is inlining the definition of `dict` with `-O1`. This means that your transformed program looks like, {{{#!hs dict' <- replicateM n ((\(k:v:_) -> (k,v)) <$> words <$> getLine) count <- length <$> catMaybes <$> replicateM q (flip M.lookup (M.fromList dict) <$> getLine) }}} Meaning that the `Map` is being reconstructed with every line that is read. You can easily discourage GHC from performing this inlining by placing a bang on the `dict'` binding, {{{#!hs !dict' <- M.fromList <$> replicateM n ((\(k:v:_) -> (k,v)) <$> words <$> getLine) }}} You were accomplishing this same end with your `evaluate $ deepseq`, but more "forcefully". Indeed it is a bit unfortunate that GHC decides that this inlining is beneficial, but I'm not entirely sure how it could know otherwise. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11677#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler