lua-users home
lua-l archive

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


2013/3/31 Marco Salamone <requerent@gmail.com>:

> I've read that a Lua program IS a table.

Close, but not quite.

A running Lua program is a thread (which is one of the eight types)
with a global table (which is one of the eight types) and a stack
(which is not).

> It seems like all data is or could be perfectly well-managed. Why is there a
> need for a GC? When we leave scope all local variables get freed and
> whenever a table is nil'ed we're implicitly freeing that memory.

It is inefficent to do all that immediately. The GC waits until there
is enough garbage around to make the effort worthwhile.

> This leads me to another point of conjecture in regards to memory
> management. This is more of a general question about language design. How
> are static values stored in memory? Suppose I have an expression,
> X = 10

Each Lua chunk has a stack of constants. Each item on that stack
is a TValue, which in most implememtations is 8 characters long.

> In C, we have a zillion different integer types to wrap memory allocation
> expectations into the definition. That all makes sense, but in the above
> expression, we don't need more than 4-bits. In C, we can malloc 4 bits of
> space, assign it to a variable, and just treat it like an integer- afaik,
> the type declarations in plain old C are not much more than syntactic sugar.

This is not true, and in fact this is as far as I'm reading your post,
since anyone who thinks that about C does not have enough background
to understand Lua implementation.