lua-users home
lua-l archive

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

Edgar Toernig wrote:
> > As the subject says, I've implemented a per-tag explicit garbage collection.
> >...
> > I'd like to know what people think of this, and whether or not people think
> > this, or something like it should go into a future version of Lua.
> Ehh... what's the point?  You do all the dirty work of walking the whole
> heap to determine what objects are stale and when it's actually time to
> free stuff you stop and only free userdata objects with some special tag.
> You save time by not freeing objects - strange decision ;-)

The point was to only garbage collect objects with a given tag.  The idea was to 
reduce the gc overhead by avoiding gc of other tags that can happen in their own 

You are right in that I am still calling markall(L) and therefore am doing all the 
work of a full garbage collection while only saving the actual destruction of 
objects.  The point was to prove that functionally, it could be done, not 
necessarily that it be the most correct or ideal way to do it.  I don't fully 
understand the Lua implementation yet, so I was quite happy to just be able to pull 
off this hack.

I tried to substitute markall(L) with:

    GCState st;
    int e;

    st.cmark = NULL;
    st.tmark = L->gt;  /* put table of globals in mark list */
    L->gt->mark = NULL;

#if 0
    marktagmethods(L, &st);  /* mark tag methods */
    for (e=0; e<TM_N; e++) {
        Closure *cl = luaT_gettm(L, tag, e);
        if (cl) markclosure(&st, cl);

with me changing the #if 0 in there to #if 1, but both results in an incorrect 
behavior of actually freeing objects which still have a reference to them.  I am not 
quite able to figure it out.

> Beside that, you'll have some problems with already collected userdata
> objects still hanging around in TMtable[].collected.  They are disconnected
> from the userdata hash and a pushuserdata will create a new object.  That
> way you may get multiple objects with same userdata-value.  The gc method
> may be confused...

I don't know how the algorithm works -- it looks somewhat convoluted to me.  So if 
you know how to fix it, could you explain?
> Oh, and you waste memory!  You do not unmark objects.  So the next time
> the gc runs, these objects will not be collected even if they are no
> longer in use...

Hmm ... that's not quite what I observed, but I will look at it a little closer to 
make sure that I haven't broken things in the way you suggest.

> [...]  Uhh, I think this will even bomb in some circumstances
> (userdata in table, table still marked but userdata was unmarked, next
> time table will not be scanned because already marked, userdata not marked
> again, collected later, but still reference in table, boom).

Fine, but I still want a way to garbage collect only objects with a specific tag 
(and which would ignore the threshold, or assume it was 0 or whatever just for that 

Paul Hsieh