lua-users home
lua-l archive

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

On Fri, Nov 09, 2001 at 09:20:36AM -0700, Joshua Jensen wrote:
> > Userdata is the way to send C pointers to Lua (and back). It 
> > was designed from the start for this use. Why try to avoid it? --lhf
> Because for every user data pushed to Lua, memory is allocated which
> needs to later be garbage collected.  One might argue that the userdata
> could be created once and lua_ref()'ed, and yes, it could, but it makes
> the programming far more painful for simple, changing pointer pass
> thrus.  That is why the LuaState distribution provides the LUA_TPOINTER
> type (the changes to Lua to achieve a LUA_TPOINTER type are actually
> very, very few in number).  A void* pointer can be pushed to Lua without
> anything more than a stack entry being created, just like a number.
> Although Amped didn't have the LUA_TPOINTER type, it did have a need to
> pass a pointer through Lua to a C callback.  Lua didn't need to know
> anything about the pointer... That is, it didn't need to be user data.
> Userdata is powerful, and it definitely has its uses.  I used userdata
> in Amped, but it was used at a far more limited scope than the pointer
> passing I needed to be doing.

Is there any way to get this kind of performance enhancement without
loosing the userdata safety? (i.e. being able to ask what kind of
pointer it is). Is it possible to push a tag onto the stack next to
the pointer, so that it could be more like a real userdata type?

David Jeske (N9LCA) + +