lua-users home
lua-l archive

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


> 2. Have the position vector be returned as a reference to the actual vector 
> within the matrix. Depending on how you expose userdata, and how you've 
> programmed your classes, this will be easy or difficult. If it is difficult, 
> consider a more thorough rewrite.

I created a new userdata class VectorRef that has a pointer to a vector and a reference to the parent matrix.

struct VectorRef
{
    Vector *value;
    int ref;
};

So now __index metamethod of the matrix class returns VectoRefs instead. The reference to the parent matrix keeps it from being collected while there are alive VectorRefs. I am using lua_ref/lua_unref pair to handle the reference to the parent.

The new code seems to work fine. Then I tried to measure its performance:

a=Matrix()
for i=1,100000 do b=a.pos end

The new code runs even faster than the old code that was creating a new vector. However, calling collectgarbage() now takes about 2 seconds, compared to almost nothing with the old code...

Can someone offer an explanation? Why does using lua_ref/lua_unref lead to such a slow GC? Is there a faster way?

Regards,
Ivo