Help with optimization/profiling
Haskell Community [Unofficial]
April 26, 2026
You can speed it up a lot by loading the data more directly without using binary:
{-# INLINE unconsW8 #-}
unconsW8 :: BSL.ByteString -> Maybe (Word8, BSL.ByteString)
unconsW8 = BSL.uncons
{-# INLINE unconsW16 #-}
unconsW16 :: BSL.ByteString -> Maybe (Word16, BSL.ByteString)
unconsW16 bs = do
(x, bs') <- unconsW8 bs
(y, bs'') <- unconsW8 bs'
pure (fromIntegral x .|. (fromIntegral y `shiftL` 8), bs'')
{-# INLINE unconsW32 #-}
unconsW32 :: BSL.ByteString -> Maybe (Word32, BSL.ByteString)
unconsW32 bs = do
(x, bs') <- unconsW16 bs
(y, bs'') <- unconsW16 bs'
pure (fromIntegral x .|. (fromIntegral y `shiftL` 16), bs'')
{-# INLINE unconsFloat #-}
unconsFloat :: BSL.ByteString -> Maybe (Float, BSL.ByteString)
unconsFloat bs = case unconsW32 bs of
Just (x, xs) -> Just (castWord32ToFloat x, xs)
Nothing -> Nothing
main = do
pixelsRaw <- BSL.readFile "matrix.bin"
let pixels = SV.unfoldrExactN (5000*5000) (\s -> case unconsFloat s of Nothing -> (0,s); Just (x, s') -> (x, s')) pixelsRaw
BSL.writeFile "/tmp/twotime-bsl.svg" (pageSvg pixels)
That also removes all GC overhead:
before: (click for more details) after: (click for more details)
Edit: SV.unfoldrExactN is much better than SV.replicateM
Discussion in the ATmosphere