lua-users home
lua-l archive

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


Mark Hamburg escribió:

> A simplistic approach to garbage collection for temporaries. This is a
> simplified version of the zero-reference-count table optimization for
> reference counting.

Interesting idea.

Here is an even simpler version:

Put a refcount bit in every lua_Object. 

1) When a new object is created (and put on the stack), the bit is off.

2) Just before any object is copied from the stack to anywhere else,
   the bit is unconditionally set.

3) If a stack slot is overwritten and the refcount bit for the object
   being overwritten is off, the object can be immediately deleted.

Since the definition of a lua_Object includes a one-byte type field, the 
refcount bit could actually occupy a whole byte without using any more 
space; this makes testing and setting faster. The set does not require a 
test; the write barrier in step 3 is a single instruction, so the overhead 
should be minimal. The deletion is not recursive (in this simplistic 
algorithm) because it is not possible for any referenced object to not be 
refcounted; this reduces the applicability but it's almost good enough for 
temporary objects which are not too complex (such as Dylan's vectors).

To make this actually work with Lua function calls, it would be necessary 
to define a relocate-stack-object primitive in the VM. If an object is 
simply relocated on the stack, it is not necessary to set the refcount bit 
but it would be essential to know that the slot it was being relocated 
from was not going to be used again (this could be guaranteed by setting 
the slot to nil); in the case of function returns, it is certainly true.

R.