{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreid2qr7hvn333dl7imtbz3gz6eefdraa37psh5hwl3b3f7hhnaypri",
"uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mkfhctay3fc2"
},
"path": "/t/help-with-optimization-profiling/13982#post_10",
"publishedAt": "2026-04-26T10:37:49.000Z",
"site": "https://discourse.haskell.org",
"tags": [
"(click for more details)"
],
"textContent": "You can speed it up a lot by loading the data more directly without using `binary`:\n\n\n {-# INLINE unconsW8 #-}\n unconsW8 :: BSL.ByteString -> Maybe (Word8, BSL.ByteString)\n unconsW8 = BSL.uncons\n\n {-# INLINE unconsW16 #-}\n unconsW16 :: BSL.ByteString -> Maybe (Word16, BSL.ByteString)\n unconsW16 bs = do\n (x, bs') <- unconsW8 bs\n (y, bs'') <- unconsW8 bs'\n pure (fromIntegral x .|. (fromIntegral y `shiftL` 8), bs'')\n\n {-# INLINE unconsW32 #-}\n unconsW32 :: BSL.ByteString -> Maybe (Word32, BSL.ByteString)\n unconsW32 bs = do\n (x, bs') <- unconsW16 bs\n (y, bs'') <- unconsW16 bs'\n pure (fromIntegral x .|. (fromIntegral y `shiftL` 16), bs'')\n\n {-# INLINE unconsFloat #-}\n unconsFloat :: BSL.ByteString -> Maybe (Float, BSL.ByteString)\n unconsFloat bs = case unconsW32 bs of\n Just (x, xs) -> Just (castWord32ToFloat x, xs)\n Nothing -> Nothing\n\n main = do\n pixelsRaw <- BSL.readFile \"matrix.bin\"\n let pixels = SV.unfoldrExactN (5000*5000) (\\s -> case unconsFloat s of Nothing -> (0,s); Just (x, s') -> (x, s')) pixelsRaw\n BSL.writeFile \"/tmp/twotime-bsl.svg\" (pageSvg pixels)\n\n\nThat also removes all GC overhead:\n\nbefore: (click for more details) after: (click for more details)\n\nEdit: `SV.unfoldrExactN` is much better than `SV.replicateM`",
"title": "Help with optimization/profiling"
}