lua-users home
lua-l archive

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


What's wrong with references? Basically, the issue is that they are dangerous in a number of potentially surprising ways. For example:

* They are uncollectable cycles waiting to happen. If you have a userdata that needs to refer to some other Lua object and does so via a reference -- a seemingly reasonable use of references -- that opens things up to a cycle that the garbage collector can't collect because the references root the objects in the registry. (The correct solution for these cases is to use the environment table for the referencing userdata.)

* They create false matches in multi-universe contexts. Since they are just allocated as integers within a universe, there is no good way to tell that a reference isn't being used in the right universe. It will still likely lead to an object, just the wrong object. Using a light userdata as a key means that you will end up at nil if you try to look for the matching Lua object in the wrong universe. (This could also be addressed by using a global, atomically incremented counter for generating reference id's rather than the existing reference mechanism.)

* References also create an initialization order problem with multiple universes if you have data like global tables needed for a C-based module where you try to track their identity via references. Unless all of the universes go through exactly the same initialization sequence, you will probably need different reference indices in the various universes. Again, a light userdata key is a much simpler solution. (This could also be addressed by using a global, atomically incremented counter for generating reference id's rather than the existing reference mechanism though the logic for when to increment the counter would need to be a bit different from the current reference logic.)

* If you use the patch to add a global-interpreter-lock to Lua, it won't actually work properly with the reference logic because the reference logic lives on the wrong side of the GIL. If you try to create two references at once, you will probably foul the reference system.

Mark