lua-users home
lua-l archive

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


On Mon, 03 Aug 2009 09:52:22 +0300, Juris Kalnins <juris@mt.lv> wrote:

Still, the difference between plain call and p-calls is appalling, .. But I think .. wrapping Lua API code in an outline function plus using this protected calling should be good enough solution.

Finally arrived at a good enough solution that avoids the overhead of the lua_cpcall,
and doesn't require heavy source refactoring.

Too bad it doesn't seem possible to do without exposing at least some internals.


Timings:

---
lua_cpcall: 1.070000
luatry + call: 0.990000
luatry: 0.690000
lua_pcall: 0.990000
lua_call: 0.690000
myluacpcall: 1.680000
lua_xcpcall: 1.350000
---

where "luatry + call:" is:

for(i=0; i<N; i++) {
    LUATRY_BEGIN(L);
    lua_pushvalue(L, 2);
    lua_pushinteger(L, i);
    lua_call(L, 1,0);
    LUATRY_CATCH;
    printf("gettop: %d\n", lua_gettop(L));
    printf("error:%s, %s\n", lua_tostring(L, -2), lua_tostring(L, -1));
    lua_settop(L, 2);
    LUATRY_END;
}

and "luatry:" has the C function inlined in the loop (i.e. what I need).

I split the work done by lua_pcall in two halves, before and after setjmp, and
added wrappers, like this:

LUA_API void lua_penter (lua_State *L, struct lua_ProtInfo *);
LUA_API void lua_pleave (struct lua_ProtInfo *);

#define LUATRY_BEGIN(L) \ { \ struct lua_ProtInfo luaprotinfo; \ lua_penter((L), &luaprotinfo); \ if (!luai_setjmp(luaprotinfo.b)) { \
        (void) 0

/* catch clause is optional*/
#define LUATRY_CATCH \ } else { \ lua_pleave(&luaprotinfo); \
            (void) 0

#define LUATRY_END \ } \ lua_pleave(&luaprotinfo); \ } \
    (void) 0