lua-users home
lua-l archive

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


Mike Pall wrote:

Hi,

Mark Hamburg wrote:
Only between two Lua strings. If one of the strings is coming from C, it
needs to at least go through strcmp if not being actually converted to a Lua
string.

In fact when I was profiling Diego's Luasocket, I noticed that one of my
test cases was spending 12% of its time in luaS_newlstr() (due to the
specific way Luasocket checks its userdata arguments). Converting C strings
to Lua strings all the time is pretty expensive.

I think the fastest solution is to register all methods with an upvalue
that holds a reference to the metatable. Storing and retrieving the
metatable to/from the registry is not necessary. And you don't have to
provide a unique name or a unique memory address for all of your userdata
types.

The problem with that solution is that it complicates greatly the registration of functions, because you have to think in advance of all types that are going to be checked by your function (for example let's say that your function takes 10 arguments, each of different types). You would need to have 10 upvalues for your function, furthermore you would need to remember which upvalue index corresponds to which type.

However, a similar idea would be to create lua references on the metatables with lua_ref , then you use lua_rawequals like you suggested. However I have no idea of the efficiency of lua_ref / lua_getref functions (haven't looked at the code yet).