lua-users home
lua-l archive

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

On Tue, Sep 29, 2015 at 09:44:43PM -0300, Soni L. wrote:
> The registry, a table, is SLOW. Can we use upvalues instead?

Many people use upvalues from C functions instead of the registry for
commonly accessed values. I sometimes use upvalues to store metatables to
implement a faster version of luaL_checkudata and luaL_testudata. But I got
the idea from others on this list.

Some reasons not to use upvalues:

1) It's slightly more complicated from C code. You can't lazily initialize
the reference as when using luaL_ref. And you can't use the luaL_Reg

2) The registry allows for shared access to a value without communicating
the runtime reference ahead of time. For example, it's common to push a
pointer to a statically scoped variable in a C module as a lightuserdata for
the registry index. Such a pointer is guaranteed unique, and doesn't require
that each C function you create in the module share the same upvalue
reference. And like luaL_Ref, it doesn't require pushing a string on the
stack, which can be comparatively slow (though Lua 5.3 has some
optimizations in this regard). (Are you currently use luaL_Reg or the
lightuserdata method, or are you using strings as indexes in the registry?)

3) In Lua 5.2+ a C function with upvalues is a collectable object that adds
work for the GC, whereas without upvalues a C function is just a pointer,
like lightuserdata.

4) Lua uses tables heavily. If table access is too slow as a general matter,
then maybe Lua (or at least the PUC Lua VM) isn't the right language for