lua: now we have a function to static allocate an array

lua_newtable() definition:
	#define lua_newtable(L) 	lua_createtable(L, 0, 0)

This simply allocates space for 0 fields, meaning, if we push any
values into this array, it will re-allocate the array, which is bad.

This function statically allocates an array, so it's ready to have
X fields into it.

Performance tests:

lua_newtable
1428161
1426992
1413513

lua_createtable
2004544
1974117
1957533

These tests were done on an AMD 8350fx CPU, single thread used.

narr: This is for fields that just have an index, e.g. arr[0] etc.
nrec: For fields which needs like arr.a, arr.b etc.

This is how many times each of the functions can run per second, as
you can see about 1.7x the calls to lua_newtable.

All credits goes to @dalkon, he was too lazy to do it by himself, and
asked me to do it for him.
This commit is contained in:
Ahmed Samy 2013-12-21 16:13:24 +02:00
parent 24e26e190b
commit df3546b073
3 changed files with 10 additions and 4 deletions

View File

@ -986,6 +986,11 @@ void LuaInterface::newTable()
lua_newtable(L); lua_newtable(L);
} }
void LuaInterface::createTable(int narr, int nrec)
{
lua_createtable(L, narr, nrec);
}
void* LuaInterface::newUserdata(int size) void* LuaInterface::newUserdata(int size)
{ {
return lua_newuserdata(L, size); return lua_newuserdata(L, size);

View File

@ -274,6 +274,7 @@ public:
void rawSeti(int n, int index = -2); void rawSeti(int n, int index = -2);
void newTable(); void newTable();
void createTable(int narr, int nrec);
void* newUserdata(int size); void* newUserdata(int size);
void pop(int n = 1); void pop(int n = 1);

View File

@ -96,7 +96,7 @@ int push_luavalue(const LuaCppFunction& func)
// color // color
int push_luavalue(const Color& color) int push_luavalue(const Color& color)
{ {
g_lua.newTable(); g_lua.createTable(0, 4);
g_lua.pushInteger(color.r()); g_lua.pushInteger(color.r());
g_lua.setField("r"); g_lua.setField("r");
g_lua.pushInteger(color.g()); g_lua.pushInteger(color.g());
@ -132,7 +132,7 @@ bool luavalue_cast(int index, Color& color)
// rect // rect
int push_luavalue(const Rect& rect) int push_luavalue(const Rect& rect)
{ {
g_lua.newTable(); g_lua.createTable(0, 4);
g_lua.pushInteger(rect.x()); g_lua.pushInteger(rect.x());
g_lua.setField("x"); g_lua.setField("x");
g_lua.pushInteger(rect.y()); g_lua.pushInteger(rect.y());
@ -168,7 +168,7 @@ bool luavalue_cast(int index, Rect& rect)
// point // point
int push_luavalue(const Point& point) int push_luavalue(const Point& point)
{ {
g_lua.newTable(); g_lua.createTable(0, 2);
g_lua.pushInteger(point.x); g_lua.pushInteger(point.x);
g_lua.setField("x"); g_lua.setField("x");
g_lua.pushInteger(point.y); g_lua.pushInteger(point.y);
@ -196,7 +196,7 @@ bool luavalue_cast(int index, Point& point)
// size // size
int push_luavalue(const Size& size) int push_luavalue(const Size& size)
{ {
g_lua.newTable(); g_lua.createTable(0, 2);
g_lua.pushInteger(size.width()); g_lua.pushInteger(size.width());
g_lua.setField("width"); g_lua.setField("width");
g_lua.pushInteger(size.height()); g_lua.pushInteger(size.height());