From 773d58da016b31557b30b7ec6065b6fe360b37b5 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Wed, 1 Aug 2012 21:08:12 -0300 Subject: [PATCH] Optimize lua object pushing --- src/framework/luaengine/luaobject.cpp | 33 ++++++++++++++++----------- src/framework/luaengine/luaobject.h | 7 +----- src/main.cpp | 3 +++ src/otclient/item.h | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/framework/luaengine/luaobject.cpp b/src/framework/luaengine/luaobject.cpp index 68de550d..ce94f53e 100644 --- a/src/framework/luaengine/luaobject.cpp +++ b/src/framework/luaengine/luaobject.cpp @@ -23,11 +23,11 @@ #include "luaobject.h" #include "luainterface.h" +#include #include LuaObject::LuaObject() : - m_fieldsTableRef(-1), - m_metatableRef(-1) + m_fieldsTableRef(-1) { } @@ -37,11 +37,6 @@ LuaObject::~LuaObject() assert(!g_app.isTerminated()); #endif releaseLuaFieldsTable(); - - if(m_metatableRef != -1) { - g_lua.unref(m_metatableRef); - m_metatableRef = -1; - } } bool LuaObject::hasLuaField(const std::string& field) @@ -91,13 +86,19 @@ void LuaObject::luaGetField(const std::string& key) void LuaObject::luaGetMetatable() { - if(m_metatableRef == -1) { - // set the userdata metatable - g_lua.getGlobal(stdext::format("%s_mt", getClassName())); - m_metatableRef = g_lua.ref(); - } + static std::unordered_map metatableMap; + const std::type_info& tinfo = typeid(*this); + auto it = metatableMap.find(&tinfo); - g_lua.getRef(m_metatableRef); + int metatableRef; + if(it == metatableMap.end()) { + g_lua.getGlobal(getClassName() + "_mt"); + metatableRef = g_lua.ref(); + metatableMap[&tinfo] = metatableRef; + } else + metatableRef = it->second; + + g_lua.getRef(metatableRef); } void LuaObject::luaGetFieldsTable() @@ -112,3 +113,9 @@ int LuaObject::getUseCount() { return ref_count(); } + +std::string LuaObject::getClassName() +{ + // TODO: this could be cached for more performance + return stdext::demangle_name(typeid(*this).name()); +} diff --git a/src/framework/luaengine/luaobject.h b/src/framework/luaengine/luaobject.h index b2e61571..9c1e3b71 100644 --- a/src/framework/luaengine/luaobject.h +++ b/src/framework/luaengine/luaobject.h @@ -24,7 +24,6 @@ #define LUAOBJECT_H #include "declarations.h" -#include /// LuaObject, all script-able classes have it as base // @bindclass @@ -76,10 +75,7 @@ public: int getUseCount(); /// Returns the derived class name, its the same name used in Lua - std::string getClassName() { - // TODO: this could be cached for more performance - return stdext::demangle_name(typeid(*this).name()); - } + std::string getClassName(); LuaObjectPtr asLuaObject() { return static_self_cast(); } @@ -87,7 +83,6 @@ public: private: int m_fieldsTableRef; - int m_metatableRef; }; #include "luainterface.h" diff --git a/src/main.cpp b/src/main.cpp index c3162f42..14a3e85f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,9 @@ #include #include +#include "otclient/item.h" +#include "otclient/tile.h" + int main(int argc, const char* argv[]) { std::vector args(argv, argv + argc); diff --git a/src/otclient/item.h b/src/otclient/item.h index 258d7dbb..29f7a7c9 100644 --- a/src/otclient/item.h +++ b/src/otclient/item.h @@ -127,7 +127,7 @@ private: uint8 m_countOrSubType; stdext::packed_storage m_attribs; }; + #pragma pack(pop) - #endif