<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">How is the performance when there is no fusion to be had?<br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">
Many of the fused operations in GHC.List take care to rewrite back to simple definitions when fusion doesn't happen. I don't know if there's still a real motivation behind that, but it's important to check.<br>
</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jul 21, 2014 at 10:29 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">`takeWhile` doesn't do the stream fusion thing. This definition seems<br>
to fix that, at least to a great extent. I don't know how to write the<br>
necessary rules to make this definition be used in the right places.<br>
<br>
takeWhile :: forall a . (a -> Bool) -> [a] -> [a]<br>
takeWhile p xs = build tw'<br>
  where<br>
    tw' :: forall b . (a -> b -> b) -> b -> b<br>
    tw' kons knil = foldr go knil xs<br>
      where<br>
        go x rest | p x       = x `kons` rest<br>
                  | otherwise = knil<br>
<br>
<br>
Tests (performed on 7.8.3, but the definition hasn't changed in HEAD):<br>
<br>
In the trivial<br>
<br>
main = print $ length $ takeWhile (< 10000000) [(1::Int) .. 20000000]<br>
<br>
it fused completely, allocating virtually nothing, whereas using<br>
Prelude.takeWhile (on 7.8.3) required a whopping 1,360,049,352<br>
<br>
In the more complex<br>
<br>
main = print $ length $ filter (\x -> x `rem` 3 == 0) $ takeWhile (<<br>
10000000) $ filter even  [(1::Int) .. 20000000]<br>
<br>
it fused partially, allocating 266,716,048 bytes.<br>
<br>
The current GHC definition allocates 746,716,000 bytes in this case<br>
(strangely, less than it used for the simple test)!<br>
<span class="HOEnZb"><font color="#888888"><br>
David Feuer<br>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
</font></span></blockquote></div><br></div>