RE: Isn't this tail recursive?
On 10 Mar 2002, Jyrinx wrote:
In the case expression at the end of countAll, each of the values looks to me like a recursive tail call - I should think (hope?) that it would be optimized by GHC into a goto statement (a la Scheme). Instead, my program eats up memory (I've got 256 MB) until the RTS whines about a stack overflow.
It is tail recusive. unfortunately, that's not the problem. apparently ghc is not smart enough to realize that countAll' should really be strict in basically all arguments. (hell, I'm not quite sure I can claim to be smart enough to say that!)
The function as written is only strict in its list argument, and its usage site only demands the 'l' argument strictly. So unless the compiler were to make use of the "can't fail" property of '+' on Int (which might not even hold if overflow checking is used), the compiler can't possibly evaluate the accumulating parameters of countAll' strictly. It would be possible to do strict evaluation in the case that the suspended computation is known to take a small bounded amount of time and space and can't fail - GHC doesn't do this, but we've wondered about it from time to time. I do wonder how often similar patterns crop up in practice - I've certainly encountered this pattern in my own code several times, and solved it using seq or strict constructor fields.
Never fear, -fall-strict is here!
I had no idea this flag still worked. As I recall, it was an experiment that turned out to be a bad idea - you're probably just better off using seq. Cheers, Simon
Looking at the online GHC Users Guide, this flag is documented in 4.19.14. Individual optimisations This has a link to Section 4.11.2, but -fall-strict isn't described in that section. Presumably its only been half removed (added) from the documentation. k Simon Marlow writes:
On 10 Mar 2002, Jyrinx wrote:
Never fear, -fall-strict is here!
I had no idea this flag still worked. As I recall, it was an experiment that turned out to be a bad idea - you're probably just better off using seq.
Cheers, Simon _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
It would be possible to do strict evaluation in the case that the suspended computation is known to take a small bounded amount of time and space and can't fail - GHC doesn't do this, but we've wondered about it from time to time.
I wonder if this would have the side-effect of making Haskell efficiency even more inscrutable. "Ah, yes, you would have expected this to be a bounded, non-failing computation but <insert reason wy this is a fragile analysis here>" Difficult tradeoff: compiler that optimizes most of your code vs. baffling changes in performance for minor changes in how the code is written. But it's not just Haskell users that get this - anyone using a modern processor can experience cache misses (huge performance cost) in C. -- Alastair Reid
participants (3)
-
Alastair David Reid -
Kevin Glynn -
Simon Marlow