Worse performance with -O
See master ticket #1168
Summary
The following program reads a sequence of integers, puts them in an array, and prints them by index.
import Control.Monad
import Data.Array.Unboxed
main :: IO ()
main = do
n <- readLn
xs <- map read . words <$> getLine
let a = listArray (1, n) xs :: UArray Int Int
q <- readLn
replicateM_ q $ do
i <- readLn
print (a!i)
Surprisingly, this runs a lot worse with -O
than without.
From the core it appears that with -O
the array is built on every step inside the replicateM_
. This turns a simple O(n+q) program into O(nq).
Steps to reproduce
Compile and run the above program with -O
.
Expected behavior
The array is constructed once.
Environment
- GHC version used: 9.2.5
Optional:
- Operating System: Ubuntu
- System Architecture: x86_64