<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">I have a (dummy) program that reads the contents of a file into a ByteString and then converts the ByteString to a Vector of Floats. To test the laziness of this program, I’ve taken a slice of the the Vector and printed it to the standard output.</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">The program looks like this:</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>import qualified Data.Vector.Unboxed as V</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>import qualified Data.ByteString as BS</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>import Data.Word</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>import System.Environment</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>import GHC.Int</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>main = do</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""> <span class="Apple-tab-span" style="white-space:pre"> </span>[file] <- getArgs</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""> <span class="Apple-tab-span" style="white-space:pre"> </span>samples <- getSamplesFromFile file</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""> <span class="Apple-tab-span" style="white-space:pre"> </span>let slice = V.slice 0 50000 samples</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""> <span class="Apple-tab-span" style="white-space:pre"> </span>print slice</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><span class="Apple-tab-span" style="white-space:pre"> </span><br class="webkit-block-placeholder"></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>getSamplesFromFile = fmap toVector . BS.readFile </div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>toVector :: BS.ByteString -> V.Vector Float</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>toVector bs = vgenerate (fromIntegral (BS.length bs `div` 3)) $ \i -></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span>myToFloat [BS.index bs (3*i), BS.index bs (3*i+1), BS.index bs (3*i+2)]</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span> where</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""> <span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span> myToFloat :: [Word8] -> Float</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""> <span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span> myToFloat = sum . map fromIntegral </div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>vgenerate n f = V.generate n (f . fromIntegral)</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">So I compile this program and generate a memory profile via:</div>
<ol class="">
<li style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">ghc Main.hs -O2 -rtsopts -prof </li>
<li style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">./Main debug48.wav +RTS -hy</li>
<li style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">hp2ps -e8in -c Main.hp</li>
<li style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">ps2pdf Main.ps</li>
</ol><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">The file <i class="">debug48.wav </i> is a 12.9MB file.</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">This is the result:</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><img apple-inline="yes" id="C3FB7863-CF87-4DE4-8E48-37128BA12967" class="" src="cid:60C4BB1D-D32D-4BDB-80BB-DA5241E78B3E@kotnet.org"></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">If I now create a cabal project and add the exact same program as the main file by:</div><div style="margin: 0px; line-height: normal;" class=""><ol class="MailOutline"><li class=""><font color="#454545" face="Helvetica Neue" class="">mkdir testing</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">cd testing</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">cabal init</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">add the program’s code to src/Main.hs</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">add bytestring and vector to the build dependencies (in testing.cabal)</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">add -O2 -rtsopts -prof to the ghc-options (in testing.cabal)</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">cabal install</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">testing debug48.wav +RTS -hy</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">hp2ps -e8in -c testing.hp</font></li><li class=""><font color="#454545" face="Helvetica Neue" class="">ps2pdf testing.ps</font></li></ol><div class=""><font color="#454545" face="Helvetica Neue" class="">This is the result:</font></div><div class=""><br class=""></div><div class=""><img apple-inline="yes" id="EFBEAC6E-F142-49D7-B386-04B1A6ED3589" class="" src="cid:B0D6AB65-D19D-4F51-8294-13DAEAD2BB8B@kotnet.org"></div></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">How can there be such a big difference in memory usage, just by the code being part of a cabal project?</div></div></body></html>