External Publication
Visit Post

Help with optimization/profiling

Haskell Community [Unofficial] April 25, 2026
Source

I have a Haskell program that generates an .svg file with an embedded (base64-encoded) png in it.

For a 5000x5000 image it takes about 2s to generate the svg. I’m not sure if I can get it any faster, so I created a minimal test case and ran it with profiling on.

speedscope gave the following results:

So clearly my imageBytes function is the main performance culprit. The left half of it is just JuicyPixels’ png generation at work. The right half is time spent in the function itself (I think). The function itself is very simple though:

imageBytes :: SizedImage -> (Float -> Pixel8) -> BSL.ByteString
imageBytes img convertPixel = do
    let image =
            generateImage
                (\x y -> convertPixel $ img.entries SV.! ((img.height - y - 1) * img.width + x))
                img.width
                img.height
    case encodePalettedPng viridisPalette image of
        Right v -> v
        Left e -> error e

(convertPixel takes an entry from img.entries :: Vector Float and normalizes it using the min/max pixel value in the image).

Now I am stuck a bit. How do I go deeper into profiling? In other words, what to do after speedscope exhausted its usefulness?

Discussion in the ATmosphere

Loading comments...