lua-users home
lua-l archive

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


I have several lua-callable functions implemented in C, each of which
takes multiple parameters.

I can either pass each parameter on the lua stack, or I can create a
table, fill it with the parameters, then pass it as a single parameter
to the function.

In many cases, the table of parameters already exists (e.g. a vector)
and does not need to be created on the fly.

So, my question is, in terms of performance, which is better?

Here is an example:

Method 1:  Multiple parameters on stack
---------------------------------------
Lua code:

--t is a pre-existing table
func( t.a, t.b )

C code:

static int func( lua_State* L )
{
    int a = luaL_check_int( L, 1 );
    int b = luaL_check_int( L, 2 );

    return 0;
}

Method 2:  Single table containing parameters
---------------------------------------------
Lua code:

--t is a pre-existing table
func( t )

C code:

static int func( lua_State* L )
{
    lua_pushliteral( L, "a" );
    lua_gettable( L, 1 )
    int a = luaL_check_int( L, -1 );

    lua_pushliteral( L, "b" );
    lua_gettable( L, 1 )
    int b = luaL_check_int( L, -1 );

    return 0;
}

By appearances the first example, I believe, involves more work on the
lua side, while the second example involves more work on the C side.

So, which is optimal?

-Kevin