lua-users home
lua-l archive

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


On Fri, Aug 1, 2014 at 12:16 PM, Thiago L. <fakedme@gmail.com> wrote:
>
> On 01/08/14 04:13 PM, Coda Highland wrote:
>>
>> On Fri, Aug 1, 2014 at 12:08 PM, Dirk Laurie <dirk.laurie@gmail.com>
>> wrote:
>>>
>>> If there is no API function for this, then there is no call for having
>>> it in Lua.
>>>
>>> What would such an API function do? Traverse the whole table and
>>> set every value to nil, in order to adjust the reference count. It's not
>>> obvious that the performance enhancement relative to
>>>
>>> for k in pairs(tbl) do tbl[k]=nil end
>>>
>>> is worth yet another library function in a language that can't even carry
>>> the baggage of cosh, sinh and tanh.
>>
>> Is that the only way to achieve it? Is there not a way to simply
>> disown the contents of the table and leave it for GC? I mean, even if
>> it's "create new empty table, swap internal data pointers, let new
>> table go unreferenced" that would be faster than a loop -- and it
>> would be something that couldn't be achieved by Lua code.
>>
>> /s/ Adam
>>
> You mean something like:
> setmetatable(t,{__mode="kv"}) collectgarbage() setmetatable(t,nil) for k in
> pairs(tbl) do tbl[k]=nil end
>

No, I mean something like:

Table* junk = /* however you make a new Table object */;
TValue* array = junk->array;
Node* node = junk->node;
Node* lastfree = junk->lastfree;
junk->array = tbl->array;
junk->node = tbl->node;
junk->lastfree = tbl->lastfree;
tbl->array = array;
tbl->node = node;
tbl->lastfree = lastfree;

I have no idea if this would actually work or not, but it's the kind
of thing I'm talking about. O(1) behavior for the clear() operation
itself, and the GC goes about its business the normal way.

/s/ Adam