Twinside / Juicy.Pixels

Haskell library to load & save pictures

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dropAlphaLayer - big allocations rate

AskAlexSharov opened this issue · comments

Input: 1200x12000 image
Produce 1 Billion allocations

Benchmark code:

module Main where

import           Codec.Picture
import           Codec.Picture.Types
import           Control.Exception
import           Criterion
import           Criterion.Main      (defaultMain)
import           Weigh

main :: IO ()
main = do
  f1 <- readPng "test-png-original.png"
  let img = toRgba8 $ unpack f1

  -- bench time
  defaultMain
    [ bgroup
        "lib"
        [ bench "dropAlphaLayer" $ whnf dropAlphaLayer img
        ]
    ]

  -- bench memory
  mainWith (wgroup "lib" (do
      func "dropAlphaLayer" dropAlphaLayer img
    ))

unpack :: Either String DynamicImage -> DynamicImage
unpack eitherImg =
  case eitherImg of
    Left e         -> throw (WrongFile e)
    Right original -> original

toRgba8 :: DynamicImage -> Image PixelRGBA8
toRgba8 dynImage =
  case dynImage of
    ImageRGBA8 img  -> img
    _ -> undefined

Results:

  Case              Allocated  GCs
  dropAlphaLayer  960,478,912  918

Hmm... Looks like "allocations" in Haskell world has another meaning (it including heap and stack allocations). Will close ticket and try more profiling techniques from http://book.realworldhaskell.org/read/profiling-and-optimization.html