|
Am 20.04.2014 14:55 schröbte Dirk Laurie:
2014-04-20 13:50 GMT+02:00 Peng Zhicheng <pengzhicheng1986@gmail.com>:I feel it too much overhead using the full userdata just to pass some opaque values, and I need to set a per-instance metatable everytime I produced that value.Given that you will have a metatable per instance
You won't. He just adds additional lightuserdata types (which as usual share a common metatable). He needs an additional array of metatables in the global state (although that could be merged with the current metatable array) plus some counters. Lua Values get an additional integer which makes them 4 bytes larger than currently (on i386, no change on amd64, plus you can't use the NaN trick anymore, but in Lua 5.3 you cannot anyway), although one could probably merge the extra type tag with the existing tag (after all only very few values are used).
please explain (more concisely than the original post please) just how much overhead relative to a full userdata you think you are saving.
In Lua 5.3 on i386 (with the tweaking described above) you would save all of the extra allocated memory for the userdata (24 bytes per userdata, minus 16 bytes once for additional fields in the global state), only stack slots/table entries remain (12 bytes each). If you allocate a separate type for every lightuserdata you loose 4 bytes of those savings (for the metatable pointer).
I'm not sure how useful the proposal is in general, but I don't think that memory overhead is a concern here (for Lua 5.3 or on amd64 where you can't use the NaN trick anyway).
Philipp