lua-users home
lua-l archive

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


Mark Meijer wrote:
> -- Now empty the table again
> for i = 1, 1000000 do foo[i] = nil; end

You are setting the values to nil. The keys stay there. In this
case the keys are implicit, since it's all stored in the array
part. But the same statement holds for (say) string keys.

> I'm guessing this should not happen. Am I wrong? Any ideas as to why
> it does happen?

Tables expand automatically, but shrink only under certain
implementation-specific conditions. Under Lua 5.x this happens
only when resizing a table. This is triggered by inserting a key
which neither fits in the array nor the hash part.

E.g. try this:
  foo.x = true
or this:
  foo[3000000] = true
and you get the memory back.

Yeah, I know what comes next: you'll complain that this behaviour
is unexpected. Maybe it is, but there's no easy way to fix this
and still be able to modify values for existing keys during table
traversals (e.g. set them to nil) and at the same time keep the
garbage collector happy.

[Other languages don't allow you to modify containers at all
during traversal. Pick your poison.]

Lua does not optimize for your use case because it's not good
programming practice, anyway. You'd usually just drop the
reference to the table (and get a new one) instead of clearing
all values. It's a lot faster, too.

--Mike