Complexity of Lua stack operations (Lua C API) -


in lua c api, offers couple stack operation functions mentioned in lua's document

int   lua_gettop (lua_state *l); void  lua_settop (lua_state *l, int index); void  lua_pushvalue (lua_state *l, int index); void  lua_remove (lua_state *l, int index); void  lua_insert (lua_state *l, int index); void  lua_replace (lua_state *l, int index); 

my first question is, complexity of these function? o(1) or o(|index|)? checked lua manual , seems lua has 2 different implementations of stack (stack based , register based).

more specifically, if read , pop top 3 elements lua stack, can think of following 2 implementations, can know 1 more performant / suggested way?

solution 1 -- read , pop each value:

for (int = 0; < 3; ++i) {   values[i] = lua_tostring(lua_state, -1);   ...   lua_pop(lua_state, 1); } 

solution 2 -- read , pop all:

for (int = 0; < 3; ++i) {   values[i] = lua_tostring(lua_state, -1 - i); } ... lua_pop(lua_state, 3); 

lua 5.3 defines lua_pop as

#define lua_pop(l,n)            lua_settop(l, -(n)-1) 

and lua_settop can seen here; fill stack slots nil values can gc'd. it's o(1) arg 1.

you can read each value in o(1), , pop of n values o(n).

so, 2 approaches should equivalent.

re: reading values in o(1), function index2addr translates stack offset address.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -