[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: String Constant C API for Lua 5.3
- From: William Ahern <william@...>
- Date: Sat, 28 Jun 2014 21:04:22 -0700
Something I've always wanted to see in Lua was the ability to push constant
strings onto the stack using an integer identifier rather than for Lua to
recompute the hash.
For example, consider the type() builtin. In C, lua_type returns a constant
integer, which makes conditionals quick.
In Lua, type() is implemented as
lua_pushstring(L, lua_typename(L, lua_type(L, 1)))
The string hashing makes using type() in conditionals more than 4x slower
than an integer compare because it cannot benefit from Lua's string
It would be nice if there were an interface like
LUA_CSTRING, // "string"
LUA_CTABLE, // "table"
LUA_CFUNCTION, // "function"
LUA_CUSERDATA, // "userdata"
void lua_pushconstant(L, int);
where Lua would use a mapping built when the VM was instantiated to quickly
push the string onto the stack.
I was originally going to suggest a more generic API that allowed deriving
an integer identifier for any string, but I remembered that user C code can
always just use upvalues for caching strings. I already do that for
metatables in places where it matters. luaB_type could also simply be
rewritten to use upvalue memoization.