[GHC] #10825: Poor performance of optimized code.

GHC ghc-devs at haskell.org
Tue Sep 1 20:39:36 UTC 2015


#10825: Poor performance of optimized code.
-------------------------------------+-------------------------------------
              Reporter:  AndriusS    |             Owner:
                  Type:  bug         |            Status:  new
              Priority:  normal      |         Milestone:
             Component:  Compiler    |           Version:  7.8.4
              Keywords:              |  Operating System:  Unknown/Multiple
          Architecture:              |   Type of failure:  Runtime
  Unknown/Multiple                   |  performance bug
             Test Case:              |        Blocked By:
              Blocking:              |   Related Tickets:  #8814, #8835
Differential Revisions:              |
-------------------------------------+-------------------------------------
 This is possible duplicate of #8814, #8835. I am creating new ticket as I
 have fairly small example with no dependencies.

 The problem is that optimized code runs orders of magnitude slower than
 non-optimized.

 {{{#!hs
 import System.IO.Unsafe (unsafePerformIO)

 newtype MyList = MyList Int

 equalsLast :: MyList -> Int -> Bool
 equalsLast (MyList value) = (== value)

 fromList :: [Int] -> MyList
 fromList = MyList . last

 {- minimizing imports -}
 (<$>) = fmap
 replicateM' n x = sequence (replicate n x)

 readInt = read <$> getLine :: IO Int

 readAndMap :: Int -> (Int->Bool) -> IO [Bool]
 readAndMap n f = replicateM' n (f <$> readInt)
 {- if readAndMap is replaced by commented out line below, the problem goes
 away -}
 --readAndMap n f = map f <$> replicateM' n (readInt)


 fromList' :: [Int] -> MyList
 {- an attempt to further demostrate the issue. fromList'=fromList is very
 slow as well -}
 fromList' list = unsafePerformIO $ do
     putStrLn "fromList"
     return $ fromList list

 readMyList :: Int -> IO MyList
 readMyList n = fromList' <$> (replicateM' n $ read <$> getLine)

 main :: IO ()
 main = do
     n1 <- read <$> getLine :: IO Int
     n2 <- read <$> getLine :: IO Int
     l <- readMyList n1
     ans <- readAndMap n2 (equalsLast l)
     mapM_ print ans
 }}}

 If this code is compiled with no flags, it prints "fromList" once and runs
 in 0.3s. Compiled with "-O" prints "fromList" many times and takes over 6
 seconds.

 Here's a Haskell script to generate input for program above:
 {{{#!hs
 main = sequence_ $ map print (n:n:[1..n]++[1..n]) where n = 10000
 }}}

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


More information about the ghc-tickets mailing list