[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Fast clone (shallow copy) a table
- From: Dirk Laurie <dirk.laurie@...>
- Date: Tue, 4 Jul 2017 06:37:41 +0200
2017-07-04 6:02 GMT+02:00 云风 Cloud Wu <cloudwu@gmail.com>:
> 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 ?
Let's get this straight: you are not offering code that relies on
#include <lobject.h>
#include <ltable.h>
as if doing so is kosher; you are merely providing proof-of-concept
code that the requested feature lua_clonetable is possible, right?