I am writing a program to analyze .TGA files using Haskell - however, the performance is absolutely terrible. A 2048 x 2048 image takes a few seconds to parse.
I ran my code using +RTS -p -RTS and got the following interesting snippets from the report:
total time = 1.08 secs total alloc = 3,037,568,120 bytes COST CENTRE MODULE %time %alloc readPixelMap Main 33.0 11.0 readPixelMap.getByte Main 32.7 75.1 readPixelMap.getColor Main 27.0 13.3
It seems that my program allocates a huge amount of memory in the readPixelMap function. This function looks like this:
readPixelMap width height = do pixels <- replicateM height (replicateM width getColor) return $ PixMap pixels where getByte = liftM toInteger getWord8 getColor = do (red:green:blue:alpha:xs) <- replicateM 4 getByte return (red, green, blue, alpha)
and a PixMap is defined as
data PixMap = PixMap [[RGBAColor]] deriving (Show) type RGBAColor = (Integer, Integer, Integer, Integer)
readPixelMap is called using the Get monad from Data.Binary :
main = do binary <- BS.readFile "test.tga" let (pixelMap, binary', nil) = runGetState (readPixelMap 2048 2048) binary 0
I was under the impression that a 2048 x 2048.TGA image should be loaded into several hundred megabytes (no more) of memory. Is there an obvious way to improve garbage collection / placement?
optimization haskell
sdasdadas
source share