[Haskell-cafe] Re: does the compiler optimize repeated calls?

David Roundy droundy at darcs.net
Wed Sep 6 09:44:05 EDT 2006

On Wed, Sep 06, 2006 at 03:26:29PM +0200, Stephane Bortzmeyer wrote:
> On Wed, Sep 06, 2006 at 02:12:28PM +0100,
>  Sebastian Sylvan <sylvan at student.chalmers.se> wrote 
>  a message of 36 lines which said:
> > I think most compilers actually do CSE 
> And automatic memoization? Because common subexpressions can be
> difficult to recognize but, at run-time, it is much easier to
> recognize a function call that has already been done. Any common
> compiler / interpreter which automatically memoizes? In theory, it is
> also a huge advantage of "pure" functional programming languages.

Have you even considered the space costs of this? For almost any
non-trivial bit of code I've written, automatic memoization would
result in a completely useless binary, as it almost guarantees
horrific space leaks.  In order to do this, you'd need to have some
rather sophisticated analysis to figure out the memory costs of both
the output of a function, and its input, both of which would need to
be stored in order to memoize the thing.

Before something like this were to be implemented, I'd rather see
automatic strictifying for avoidence of stack overflows (i.e. when the
stack starts filling up, start evaluating things strictly), which is
most likely another insanely dificult bit of compiler code, which
would also involve figuring out which code paths would save memory,
and which would increase the memory use.
David Roundy

More information about the Haskell-Cafe mailing list