Poor performance of optimized code.
This is possible duplicate of #8814, #8835 (closed). 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.
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:
main = sequence_ $ map print (n:n:[1..n]++[1..n]) where n = 10000
Trac metadata
Trac field | Value |
---|---|
Version | 7.8.4 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | #8814, #8835 (closed) |
Blocking | |
CC | floppycat@gmail.com |
Operating system | |
Architecture |