[GHC] #11365: Worse performance with -O

#11365: Worse performance with -O -------------------------------------+------------------------------------- Reporter: | Owner: facundo.dominguez | Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: Keywords: optimization | Operating System: Unknown/Multiple performance concurrency | Architecture: | Type of failure: Runtime Unknown/Multiple | performance bug Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- The running time of the following program worsens when compiled with {{{-O}}}, and worsens more when compiled with ghc-7.10.2. {{{ #!haskell -- /opt/ghc-7.8.3/bin/ghc --make -threaded -fforce-recomp test.hs -- time ./test: 3 seconds -- -- /opt/ghc-7.8.3/bin/ghc --make -threaded -O -fforce-recomp test.hs -- time ./test: 11 seconds -- -- /opt/ghc-7.10.2/bin/ghc --make -threaded -fforce-recomp test.hs -- time ./test: 5 seconds -- -- /opt/ghc-7.10.2/bin/ghc --make -threaded -O -fforce-recomp test.hs -- time ./test: 13 seconds -- import Control.Concurrent import Control.Monad import Data.List main :: IO () main = do let x = foldl' (+) 0 [1 .. 100000000] mv <- newEmptyMVar replicateM_ 4 $ forkIO $ putMVar mv $! x nums <- replicateM 4 $ takeMVar mv print (nums :: [Integer]) }}} The following variant which doesn't share {{{x}}} improves with {{{-O}}} for ghc-7.10.2, but ghc-7.8.3 still produces a faster program. {{{ #!haskell -- /opt/ghc-7.8.3/bin/ghc --make -threaded -fforce-recomp test.hs -- time ./test: 10 seconds -- -- /opt/ghc-7.8.3/bin/ghc --make -threaded -O -fforce-recomp test.hs -- time ./test: 11 seconds -- -- /opt/ghc-7.10.2/bin/ghc --make -threaded -fforce-recomp test.hs -- time ./test: 18 seconds -- -- /opt/ghc-7.10.2/bin/ghc --make -threaded -O -fforce-recomp test.hs -- time ./test: 15 seconds -- import Control.Concurrent import Control.Monad import Data.IORef import Data.List main :: IO () main = do mv <- newEmptyMVar ref <- newIORef 0 replicateM_ 4 $ forkIO $ do i <- readIORef ref putMVar mv $! foldl' (+) i [1 .. 100000000] nums <- replicateM 4 $ takeMVar mv print (nums :: [Integer]) }}} Some related discussion [https://mail.haskell.org/pipermail/ghc- devs/2016-January/010904.html here]. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11365 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11365: Worse performance with -O -------------------------------------+------------------------------------- Reporter: | Owner: facundo.dominguez | Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: optimization | performance concurrency 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: | -------------------------------------+------------------------------------- Comment (by rwbarton): The "state hack" seems to be responsible here. Without `-O`, the argument to `replicateM_` is shared and therefore the expensive computation occurs only once. With `-O`, `replicateM_` is inlined and then due to the "state hack" GHC thinks it is okay to duplicate the expensive computation. Try building with `-fno-state-hack`. I made other small adjustments to the program in testing; you may also need `-fno-full-laziness`. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11365#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11365: Worse performance with -O -------------------------------------+------------------------------------- Reporter: | Owner: facundo.dominguez | Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: optimization | performance concurrency 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: | -------------------------------------+------------------------------------- Comment (by simonpj): Ah yes! Just search for "`replicateM`" and you'll see a raft of tickets about this one problem! If someone would like to dig further, I'd be happy to help. Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11365#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11365: Worse performance with -O -------------------------------------+------------------------------------- Reporter: | Owner: facundo.dominguez | Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: optimization | performance concurrency 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: | -------------------------------------+------------------------------------- Comment (by nomeata): #1168 has a list of related tickets, and #9388 has ideas and preliminary work on how to limit the scope of the hack. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11365#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11365: Worse performance with -O -------------------------------------+------------------------------------- Reporter: | Owner: facundo.dominguez | Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: Resolution: duplicate | Keywords: optimization | performance concurrency Operating System: Unknown/Multiple | Architecture: Type of failure: Runtime | Unknown/Multiple performance bug | Test Case: Blocked By: | Blocking: Related Tickets: #1168 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by thomie): * status: new => closed * resolution: => duplicate * related: => #1168 Comment:
Ah yes! Just search for "replicateM" and you'll see a raft of tickets about this one problem!
There is a link back to this ticket from #1168, so this example can still be found and used as a test if necessary. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11365#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC