kyren / piccolo

An experimental stackless Lua VM implemented in pure Rust
Creative Commons Zero v1.0 Universal
1.67k stars 62 forks source link

Tables need to support "dead keys" #69

Closed kyren closed 5 months ago

kyren commented 5 months ago

To support concurrent mutation of the map portion of tables during iteration, PUC-Rio Lua uses something called "dead keys".

Removed entries from tables keep their key value, and if the removed key is a GC value it is marked as a "dead key". "Dead keys" are GC keys which are not removed from the table but also not traced, and can be collected as normal. The implementation of next checks for existing keys by including these "dead keys" and checking for key equality based on pointer identity. In this way, it is always safe to remove any entry from a Lua table during normal iteration.

https://www.lua.org/source/5.4/ltable.c.html#equalkey