lua-users home
lua-l archive

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


Muc

,,.


----- Reply message -----
От: "胡昊" <hh9527@gmail.com>
Кому: "Lua mailing list" <lua-l@lists.lua.org>
Тема: String Constant C API for Lua 5.3
Дата: вс, июн 29, 2014 13:57


yes, and you can even store the metatable in the upvalues to avoid the overhead of luaL_checkudata.

2014年6月29日 下午4:38于 "云风" <cloudwu@gmail.com>写道:
You may push your constants as the upvalues of your c closure first ,
and you can use upvalue index (an integer identifier) later.

In my lua-bson lib (
https://github.com/cloudwu/lua-bson/blob/master/bson.c#L1076-L1099 ) ,
I use this way to avoid string interning .

2014-06-29 12:04 GMT+08:00 William Ahern <william@25thandclement.com>:
> 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
> interning.
>
> It would be nice if there were an interface like
>
>   enum {
>     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.
>
>



--
http://blog.codingnow.com