From df3546b07397e17f265b053eefc49c9b8ec85252 Mon Sep 17 00:00:00 2001 From: Ahmed Samy Date: Sat, 21 Dec 2013 16:13:24 +0200 Subject: [PATCH] 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. --- src/framework/luaengine/luainterface.cpp | 5 +++++ src/framework/luaengine/luainterface.h | 1 + src/framework/luaengine/luavaluecasts.cpp | 8 ++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/framework/luaengine/luainterface.cpp b/src/framework/luaengine/luainterface.cpp index 9d9ee5a1..f5a89894 100644 --- a/src/framework/luaengine/luainterface.cpp +++ b/src/framework/luaengine/luainterface.cpp @@ -986,6 +986,11 @@ void LuaInterface::newTable() lua_newtable(L); } +void LuaInterface::createTable(int narr, int nrec) +{ + lua_createtable(L, narr, nrec); +} + void* LuaInterface::newUserdata(int size) { return lua_newuserdata(L, size); diff --git a/src/framework/luaengine/luainterface.h b/src/framework/luaengine/luainterface.h index 5da496af..4b46788e 100644 --- a/src/framework/luaengine/luainterface.h +++ b/src/framework/luaengine/luainterface.h @@ -274,6 +274,7 @@ public: void rawSeti(int n, int index = -2); void newTable(); + void createTable(int narr, int nrec); void* newUserdata(int size); void pop(int n = 1); diff --git a/src/framework/luaengine/luavaluecasts.cpp b/src/framework/luaengine/luavaluecasts.cpp index ef751209..ca79ce19 100644 --- a/src/framework/luaengine/luavaluecasts.cpp +++ b/src/framework/luaengine/luavaluecasts.cpp @@ -96,7 +96,7 @@ int push_luavalue(const LuaCppFunction& func) // color int push_luavalue(const Color& color) { - g_lua.newTable(); + g_lua.createTable(0, 4); g_lua.pushInteger(color.r()); g_lua.setField("r"); g_lua.pushInteger(color.g()); @@ -132,7 +132,7 @@ bool luavalue_cast(int index, Color& color) // rect int push_luavalue(const Rect& rect) { - g_lua.newTable(); + g_lua.createTable(0, 4); g_lua.pushInteger(rect.x()); g_lua.setField("x"); g_lua.pushInteger(rect.y()); @@ -168,7 +168,7 @@ bool luavalue_cast(int index, Rect& rect) // point int push_luavalue(const Point& point) { - g_lua.newTable(); + g_lua.createTable(0, 2); g_lua.pushInteger(point.x); g_lua.setField("x"); g_lua.pushInteger(point.y); @@ -196,7 +196,7 @@ bool luavalue_cast(int index, Point& point) // size int push_luavalue(const Size& size) { - g_lua.newTable(); + g_lua.createTable(0, 2); g_lua.pushInteger(size.width()); g_lua.setField("width"); g_lua.pushInteger(size.height());