That's all you need for any kind of table provided you don't have to reset the metatable. KISS.
Well, not really... if you want to force the table to release it's memory for indexs, if it's an array-like table, then you have to write something into it. Tables don't shrink back until they're written to for array-like tables... hash-like tables never shrink back, which is why it's best to GC them if they go over ~128 indexes or so and you're not reusing them more frequently than GC.
What about making the keys/values "weak" references with the setmetatable(table, {mode = "kv"})?
If values are already collected if you erase and nil out them, and non-numerical keys are never collected... what does this do again?
What about making the keys/values "weak" references with the setmetatable(table, {mode = "kv"})?
If values are already collected if you erase and nil out them, and non-numerical keys are never collected... what does this do again?
Strings present a subtlety here: Although strings are collectible, from an implementation point of view, they are not like other collectible objects. Other objects, such as tables and functions, are created explicitly. For instance, whenever Lua evaluates {}, it creates a new table. Whenever it evaluates function () ... end, it creates a new function (a closure, actually). However, does Lua create a new string when it evaluates "a".."b"? What if there is already a string "ab" in the system? Does Lua create a new one? Can the compiler create that string before running the program? It does not matter: These are implementation details. Thus, from the programmer's point of view, strings are values, not objects. Therefore, like a number or a boolean, a string is not removed from weak tables (unless its associated value is collected).
In your example, after the GC, the table will be left with
t == {
[12] = 13,
["meh"] = true,
}
as neither the "meh" string or the true boolean can be collected. Feel free to verify this. :)
Well, not really... if you want to force the table to release it's memory for indexs, if it's an array-like table, then you have to write something into it. Tables don't shrink back until they're written to for array-like tables... hash-like tables never shrink back, which is why it's best to GC them if they go over ~128 indexes or so and you're not reusing them more frequently than GC.
If values are already collected if you erase and nil out them, and non-numerical keys are never collected... what does this do again?
If no other references are held to the values in this table, after a full GC..
{__mode = "kv"}
not
{mode = "kv"}
http://www.lua.org/pil/17.html
In your example, after the GC, the table will be left with
as neither the "meh" string or the true boolean can be collected. Feel free to verify this. :)