lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Sep 13, 2000 at 06:05:29PM +0100, Reuben Thomas wrote:
> > I have speculated, too, about the reasons why lua does not do it, and
> > I suspect that it is a tradeoff for speed. You would need to allocate
> > such environments on the heap, and that can be very costly.
> 
> What's the difference between doing this in Lua and Pascal? The latter is
> supposed to be a fairly lightweight language...

I don't get your point. AFAIK, in Pascal, nested scopes are resolved
at compile-time, not at runtime. The compiler can take all the time it
wants.

> anyway, environments are
> typically quite small (provided you identify the local variables that a
> nested function actually uses).

It is not a problem of environment size and copying variables into
it. The costs of heap allocation are primarily the time it takes to
traverse the free list, find an appropriate chunk, split it up, and
return the rest to the free list. If you have to do that on every
function invocation, the costs quickly become prohibitive.

In addition, I think that identifying the variables that a nested
function actually uses would add a lot of complexity to lua. Maybe
enough to counter its goal of keeping things simple.

There is an alternative heap allocation scheme that simply bumps a
pointer (Standard ML uses it, among other languages). It is as fast as
stack allocation and obviates the need for any kind of stack at
all. But this scheme requires a copying garbage collector, and thus
uses twice as much memory as a comparable mark-and-sweep garbage
collector.

- Christian