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.
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 ?