Fast clone (shallow copy) a table

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Fast clone (shallow copy) a table

云风 Cloud Wu
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 ?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fast clone (shallow copy) a table

Dirk Laurie-2
2017-07-04 6:02 GMT+02:00 云风 Cloud Wu <[hidden email]>:

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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fast clone (shallow copy) a table

云风 Cloud Wu

Dirk Laurie <[hidden email]>于2017年7月4日周二 下午12:38写道:
> 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>

And lgc.h lstate.h, I forgot call barrierback just now.
 
as if doing so is kosher; you are merely providing proof-of-concept
code that the requested feature lua_clonetable is possible, right?

 
 Yes.

lua_clonetable can be implement by lua_next, but the performance would be must better. And I think it would be very useful.

I remember Roberto said they were considering a table.clear function ( http://lua-users.org/lists/lua-l/2013-11/msg00669.html ) , we can use table.clone(tbl, {}) to instead of it.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fast clone (shallow copy) a table

Eric Man-3

I remember Roberto said they were considering a table.clear function ( http://lua-users.org/lists/lua-l/2013-11/msg00669.html ) , we can use table.clone(tbl, {}) to instead of it.


I think it's a good idea. I suggest "table.copy"? 

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fast clone (shallow copy) a table

Xianfu Pan
In reply to this post by 云风 Cloud Wu
Yeah, I have an implementation of table.clear;
"In my extension of LUA, comments are as // not /**/"
I bet it performs better than  luaH_next, as Cloud Wu said, performance would be the KEY.


void luaH_clear(lua_State *L, Table *h) {
//need not to barrierback, because new value is NIL;
unsigned int i;
for (i = 0; i < h->sizearray; i++)
setnilvalue(&h->array[i]);
if (h->node != dummynode) {
Node *n, *limit = gnodelast(h);
for (n = gnode(h, 0); n < limit; n++)   //traverse hash part
setnilvalue(gval(n));
}
}

2017-07-04 14:31 GMT+08:00 云风 Cloud Wu <[hidden email]>:

Dirk Laurie <[hidden email]>于2017年7月4日周二 下午12:38写道:
> 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>

And lgc.h lstate.h, I forgot call barrierback just now.
 
as if doing so is kosher; you are merely providing proof-of-concept
code that the requested feature lua_clonetable is possible, right?

 
 Yes.

lua_clonetable can be implement by lua_next, but the performance would be must better. And I think it would be very useful.

I remember Roberto said they were considering a table.clear function ( http://lua-users.org/lists/lua-l/2013-11/msg00669.html ) , we can use table.clone(tbl, {}) to instead of it.



Loading...