From: &quot;Edward Z. Yang&quot; &lt;<a href="mailto:ezyang@MIT.EDU">ezyang@MIT.EDU</a>&gt;<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<br>
Hello Aleksandar,<br>
<br>
It is possible that the iteratees library is space leaking; I recall some<br>
recent discussion to this effect.  Your example seems simple enough that<br>
you might recompile with a version of iteratees that has -auto-all enabled.<br>
Unfortunately, it&#39;s not really a safe bet to assume your libraries are<br>
leak free, and if you&#39;ve pinpointed it down to a single line, and there<br>
doesn&#39;t seem a way to squash the leak, I&#39;d bet it&#39;s the library&#39;s fault.<br>
<br>
Edward<br></blockquote><div><br>I can&#39;t reproduce the space leak here.  I tried Aleksander&#39;s original code, my iteratee version, the Ngrams version posted by Johan Tibell, and a lazy bytestring version.<br><br>my iteratee version (only f&#39; has changed from Aleksander&#39;s code):<br>
<br>f&#39; :: Monad m =&gt; I.Iteratee S.ByteString m Wordcounts<br>f&#39; = I.joinI $ (enumLinesBS I.&gt;&lt;&gt; I.filter (not . S.null)) $ I.foldl&#39; (\t s -&gt; T.insertWith (+) s 1 t) T.empty<br><br>my lazy bytestring version<br>
<br>&gt; import Data.Iteratee.Char<br>&gt; import Data.List (foldl&#39;)import Data.Char (toLower)<br>&gt; <br>&gt; import Data.Ord (comparing)<br>&gt; import Data.List (sortBy)<br>&gt; import System.Environment (getArgs)<br>

&gt; import qualified Data.ByteString.Lazy.Char8 as L<br>
&gt; import qualified Data.HashMap.Strict as T<br>&gt;<br>&gt; f&#39;2 = foldl&#39; (\t s -&gt; T.insertWith (+) s 1 t) T.empty . filter (not . L.null) . L.lines<br>&gt;<br>&gt; main2 :: IO ()<br>&gt; main2 = getArgs &gt;&gt;= L.readFile .head &gt;&gt;= print . T.keys . f&#39;2<br>
 <br></div></div>None of these leak space for me (all compiled with ghc-7.0.3 -O2).  Performance was pretty comparable for every version, although Aleksander&#39;s original did seem to have a very small edge.<br><br>As someone already pointed out, keep in mind that this will use a lot of memory anyway, unless there&#39;s a lot of repetition of words.<br>
<br>I&#39;d be happy to help track down a space leak in iteratee, but for now I&#39;m not seeing one.<br><br>Best,<br>John Lato<br>