lua-users home
lua-l archive

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

Alex Davies wrote:
Simply to save memory. A pointer to a table takes some 4 bytes (x86), where as a fully tagged value takes 12-16 bytes depending on alignment. When you consider that many people use userdata just to store 8-16 bytes, that's quite an optimization. Also by ensuring that the environment is never null, a minor time/complexity saving can be made there. (as getenv always returns a table).

I'm not sure these were the reasons.
In my case, creating an extra table per a userdata, and doing an extra table lookup per access seem to be way more expensive.

The other way around your problem is of course to use a single weak keyed table, and your userdata as keys. It'd save a fair amount of memory, but I'm not sure which is better from a gc perspective.

This is how my application was doing originally. But unfortunately, Lua tables used as dictionaries won't always reclaim memory when their entries are deleted. After several millions creations and deletions of userdata, this (empty!) table was holding > 100 MBytes of RAM that could not be reclaimed with garbage collector.