lua-users home
lua-l archive

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


Sometimes we need clone a table (object). For example, clean a table rather than create a new empty table, or clone a blueprint to init object.

I guess use memcpy would be much faster than call lua_next to iterator the table, so I wrote some code to test.

https://gist.github.com/cloudwu/a48200653b6597de0446ddb7139f62e3

It's 3x~5x faster than :

static int
copytable(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE); // to
luaL_checktype(L, 2, LUA_TTABLE); // from

lua_pushnil(L);
while (lua_next(L, 2) != 0) {
lua_pushvalue(L, -2);
lua_insert(L, -2);
lua_rawset(L, 1);
}
 lua_settop(L,1); 
return 1;
}

If lua can offer a C API like lua_clonetable would be better ?