lua-users home
lua-l archive

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


I'm writing a Lua extension that exposes container-like objects.  I can
go into the full specifics of what my extension does if you like, but
for the question I'm posing, I think it will be enough to say that my
extension will create userdata objects that have references to other
Lua objects.

My question is how to make GC work properly, but also efficiently.
Unlike Ruby and Python, Lua does not (from what I can see) let userdata
objects define their own "mark" function.  This means that all objects
that are reachable only through my container will get collected unless I
do something to prevent that.

Unfortunately my options don't look very good.  The only real option I
can see is: for each of my container objects, create a Lua table that
always contains all of the other Lua objects that this container
references.  Then put this table in the registry.  This will work (I
think), but it's quite inefficient in both space and time:

 1. It's inefficient in space, because I'm creating all these
    redundant tables.  They are redundant because they contain
    information that I already have stored in another form, and
    can recreate at will very efficiently.
 2. It's inefficient in time, because I'm paying the cost to
    keep all these tables up to date (malloc(), table resizes,
    not to mention the cache effects).

So I guess I'm wondering: is there any reason why Lua doesn't let
userdata have a custom mark function?  It would allow me to implement my
extension much more efficiently.

Josh