lua-users home
lua-l archive

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



On Mar 19, 2017, at 8:55 PM, 云风 Cloud Wu <cloudwu@gmail.com> wrote:



Tim Hill <drtimhill@gmail.com>于2017年3月20日周一 上午11:12写道:
Other than tables or full userdata (which have per-value metatables), what do you need? I’m unclear on what use a metatable on a particular string, or a light userdata would have (why would it not be a full userdata in that case?).

lightuserdata can be a sub struct of an complex C/C++ object .

For example, in 3d engine, a node of a 3d object as a lightuserdata would be better than full userdata. If you query a node of a 3d object from lua , you can create a full userdata proxy for it, but you should also cache it in uservalue because it should be the same value in next query ( __eq is not enough if you want to use it as a table key).

Using lightuserdata (with metatable) for a 3d node would simplify the bindings of 3d engine and reduce the overhead.


But a light userdata with a metatable basically *IS* a full userdata. The only difference is that the full userdata is allocated by Lua, and undergoes garbage collection .. which you ALSO want for the __gc. As you noted, the technique here is to allocate a full userdata that just holds a C pointer (aka a light userdata).

Your request won’t get you a lower overhead .. it will just get you something else that is identical to full userdata but with a different name.

—Tim