lua-users home
lua-l archive

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


I've been doing a lot of testing with this, and it seems to be in pretty
good shape now.  Some parts aren't quite as fast as they could be (such as
the sweeping of strings when one frees), but all in all, it is a pretty good
representation of Lua 5.0.1 running with a reference counted garbage
collector.  The mark-sweep collector is still there and still serves a
purpose, since table cycles aren't supported.

lua.h defines LUA_REFCOUNT to be 1.  I believe all blocks of code modified
for ref counting are in an #if LUA_REFCOUNT/#endif combo.  Setting
LUA_REFCOUNT to 0 should turn off the reference counting.

The ref counted Lua is still only available through the Subversion link
below.

Finally, performance is pretty good considering the extra overhead.  As Mark
Hamburg mentioned, there may be additional optimization techniques related
to stack manipulation.  

Mostly, I'm just pleased that I can push a new string onto the stack, call a
function, and have that string destroy itself automatically when Lua is done
with it.  When I worked on the Xbox game Amped 1, the mark-sweep garbage
collection cycle took over 60 milliseconds (as I recall).  This was so
dangerous (in terms of potential game performance blips) that a pass was
made over every Lua script to ensure no function specifically allocated
anything (i.e. no new strings, tables, closures, etc).  With the ref
counting GC, Lua could have been used in a much more natural way.  I'm
excited for the Lua 5.1 collector, but in the meantime, I'm excited to have
the ref counting collector for my use.

Thanks,
Josh

> I'd hoped to have this up a couple days ago, but I wasn't as 
> quick as I would have liked.  In any case, here is my current 
> implementation of a reference counted garbage collector for 
> Lua 5.0.1.  It is built upon the mark-sweep garbage 
> collector, because cycles aren't handled by the ref counted 
> GC.  I find I don't usually have cycles, so it isn't much of 
> an issue for me.
> 
> Some information on the changes:
> 
> * Note the setnilvalue2n, setsvalue2n, etc.  These are used 
> when setting the value for the first time to a new object.
> * Some functions now expose a lua_State *L when they didn't 
> in the past.
> This is because the lua_State is required for possible object 
> destruction when releasing a reference.
> * There is a #define LUA_REFCOUNT 1 in lua.h.  It actually 
> isn't possible, at this time, to completely turn off the ref counting.
> * lapi.c: A bunch of functions have been updated to run 
> setnilvalue on the appropriate stack entries, instead of just 
> incrementing or decrementing
> L->top.
> * lgc.c: I'm not convinced the ref counted collection is used right.
> * lobject.c/h: The bulk of the ref counting is in these two files.
> * ltable.c: luaH_remove is not correct when the node position 
> is equal to the main position (I think).
> 
> The best way to see all the updates is to diff it against the 
> Lua 5.0.1 codebase.
> 
> The code is stored in a Subversion
> (http://subversion.tigris.org/project_packages.html) 
> database.  It may be retrieved with the command:
> 
> svn co svn://24.2.105.59/root/luarc
> 
> Anyway, comments are welcome, good or bad.