lua-users home
lua-l archive

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


Am 25.11.2013 14:54 schröbte Javier Guerra Giraldez:
On Sun, Nov 24, 2013 at 6:53 AM, Mike Pall <mikelu-1311@mike.de> wrote:
It doesn't change the sizes, it doesn't
shrink or reset the table (which would be much less useful in
practice).


but at some point it would be resized, right?  what happens if there's
a big table, i clear() it, and then add a single key/value.  would it
stay big forever?  until the next garbage collection? or immediately,
as the set function detects it's way oversized?

I'm interested in that as well. I took a look at the (Lua 5.2) source, and it seems that a table is only ever resized if Lua can't find an empty slot for a new element. So if you have a clear table with large hash and array parts, no memory will be freed until you fill up one of those parts. Am I missing something?

So LuaJIT's `table.clear` should work fine for its purpose (although it's basically an alias for `for k in pairs( t ) do t[ k ] = nil end` anyway). The proposed Lua `table.clear` (or `table.wipe`) would be a way to actually release the memory owned by a clear table, but what I think would be needed more is a `table.fit` function that makes array and hash part exactly as large as needed for the values currently in the table. Something automatic via the garbage collector would be even better, of course (maybe this is a case where the lifetime info of a generational gc would be useful -- short-lived tables don't need to shrink) ...

Philipp