Recursive functions and constant parameter closures (inlining/strictness analyzer question)

Dan Doel dan.doel at
Fri May 30 00:17:44 EDT 2008

On Thursday 29 May 2008, Tyson Whitehead wrote:
> I thought this was interesting.  Is it to be expected?  Am I right in
> interpreting this to mean it was just too much for the strictness
> analyzer.  I believe the first ultimately produces significantly
> superior code, so should one always write their recursive functions such
> that the constant (functional?) parameters are first captured in a closure?
> In that vein, would it be useful if the compiler automatically
> transformed the second into the first?

I've had similar experiences. I've been working on sorting the mutable arrays 
in the new uvector library, and on at least one occasion, changing from code 

    foo f x y = ... foo f x' y'

to code like:

    foo f = loop
     where loop x y = ... loop x' y'

resulted in 50% faster code (that is, 10 seconds -> 5 seconds). It doesn't 
always make such a dramatic difference, but it seems significant enough to 
have gotten me in the habit of writing such code by default (for the sorting, 
at least, where I'm trying to squeeze out as much performance as possible). 
Unfortunately, I think the resulting code is somewhat ugly, as I occasionally 
end up with code like:

    foo a b = loop1 c
     loop1 c d = loop2 e
      where loop2 e = ...

which may or may not actually be faster with more or less nested loops, but 
trying each possibility is a bit of a pain.

If the compiler could automatically derive the more straightforward definition 
into the fast one, it'd be quite a boon, but I haven't thought very hard 
about whether there are any pitfalls to doing so.

-- Dan

More information about the Glasgow-haskell-users mailing list