Re: Function to force table to shrink?
Rett Berg <[email protected]>
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <CALW7ahzYrhWm82toRT8TzmTGxDhoZ1YJHZS5qZnQLktLLPN52A@mail.gmail.com> |
Okay, so a `clear` algorithm could: * set all keys returned by pairs() to nil * set any key (i.e. t.a) to any value, then back to nil. The table will then be effectively clear. SGTM, thanks for the tip! It would still be nice IMO to have a table.clear() which just force-deallocates the table since this would save the cost of manually setting every key to nil, but this isn't required for my use-cases. Best, - Rett On Mon, Feb 2, 2026 at 12:05 PM Roberto Ierusalimschy <[email protected]> wrote: > > > I was under the impression that PUC Lua does actually shrink tables > > sometimes, at least the hash part, but I will check the sources and test > > again (unless the authors would like to answer off the top of their heads > > ;)) so I can make some reasonable statements about what is likely to happen > > and when. > > A table will shrink if you keep adding keys to it. Adding keys will > eventually force a rehash; if, at that point, the table needs less space > than it was using, it shrinks. > > ------------------------------------------------ > local a = {} > > for i = 1, 1e5 do a[i] = true end > print(collectgarbage"count" * 1024) > > for i = 1, 1e5 do a[i] = nil end > print(collectgarbage"count" * 1024) > > a.x = true > print(collectgarbage"count" * 1024) > ------------------------------------------------ > > -- Roberto > > -- > You received this message because you are subscribed to a topic in the Google Groups "lua-l" group. > To unsubscribe from this topic, visit https://groups.google.com/d/topic/lua-l/vAcAMy7GFJY/unsubscribe. > To unsubscribe from this group and all its topics, send an email to [email protected]. > To view this discussion visit https://groups.google.com/d/msgid/lua-l/20260202190458.GB10552%40arraial.inf.puc-rio.br. -- You received this message because you are subscribed to the Google Groups "lua-l" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/lua-l/CALW7ahzYrhWm82toRT8TzmTGxDhoZ1YJHZS5qZnQLktLLPN52A%40mail.gmail.com.