lua-users home
lua-l archive

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


Patrick has it right, but you sir, have found a bug.

As Patrick said, the table has to "remember" recently deleted keys, as to provide
consistent next() behaviour.

But note that this will also fix the problem:
  -- So far so good... But now spot the difference...
  print("Around 16meg too much:", GetUsedMemory());
  foo.hashpart = true   -- no need to reassign foo to a new table)
  print("Back where we started:", GetUsedMemory());

All I had to do was make an assignment to the hash table (something that "next"
specifically prohibits), to free the empty parts.

The bug you've found though is in using a table as an array exclusively the array
will never shrink, only grow. You have to make an assignment to the hashtable to
shrink it. Case in point:

  print("Around 16meg too much:", GetUsedMemory());
  for i = 1,10000 do foo[i] = true
  print("NOT back to where we started:", GetUsedMemory());

I doubt that is the problem in your program though. (No memory is leaked - it's
always reused..)

- Alex

On Wed Jan 30 20:00 , 'Patrick Donnelly' <batrick.donnelly@gmail.com> sent:

>I'm pretty sure this has to do with tables remembering "old" keys.
>This is important for next() when you try to check the next key/value
>pair 'after' removing the last...
>
>e.g.
>
>for k in pairs(t) do t[k] = nil end
>
>Note that if you try to call next() with an invalid key (one that was
>never in the table), you get an error raised.
>
>While assigning a new table does "fix" it, there probably should be
>some sort of collection of these unused keys.
>
>Note: I'm sure some of what I said is technically misleading (due to
>my misunderstanding), but I hope the general idea is understood.
>
>-- 
>-Patrick Donnelly
>
>"One of the lessons of history is that nothing is often a good thing
>to do and always a clever thing to say."
>
>-Will Durant
>)