Optimize lua object pushing

This commit is contained in:
Eduardo Bart 2012-08-01 21:08:12 -03:00
parent 3b345cf868
commit 773d58da01
4 changed files with 25 additions and 20 deletions

View File

@ -23,11 +23,11 @@
#include "luaobject.h" #include "luaobject.h"
#include "luainterface.h" #include "luainterface.h"
#include <typeinfo>
#include <framework/core/application.h> #include <framework/core/application.h>
LuaObject::LuaObject() : LuaObject::LuaObject() :
m_fieldsTableRef(-1), m_fieldsTableRef(-1)
m_metatableRef(-1)
{ {
} }
@ -37,11 +37,6 @@ LuaObject::~LuaObject()
assert(!g_app.isTerminated()); assert(!g_app.isTerminated());
#endif #endif
releaseLuaFieldsTable(); releaseLuaFieldsTable();
if(m_metatableRef != -1) {
g_lua.unref(m_metatableRef);
m_metatableRef = -1;
}
} }
bool LuaObject::hasLuaField(const std::string& field) bool LuaObject::hasLuaField(const std::string& field)
@ -91,13 +86,19 @@ void LuaObject::luaGetField(const std::string& key)
void LuaObject::luaGetMetatable() void LuaObject::luaGetMetatable()
{ {
if(m_metatableRef == -1) { static std::unordered_map<const std::type_info*, int> metatableMap;
// set the userdata metatable const std::type_info& tinfo = typeid(*this);
g_lua.getGlobal(stdext::format("%s_mt", getClassName())); auto it = metatableMap.find(&tinfo);
m_metatableRef = g_lua.ref();
}
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() void LuaObject::luaGetFieldsTable()
@ -112,3 +113,9 @@ int LuaObject::getUseCount()
{ {
return ref_count(); return ref_count();
} }
std::string LuaObject::getClassName()
{
// TODO: this could be cached for more performance
return stdext::demangle_name(typeid(*this).name());
}

View File

@ -24,7 +24,6 @@
#define LUAOBJECT_H #define LUAOBJECT_H
#include "declarations.h" #include "declarations.h"
#include <typeinfo>
/// LuaObject, all script-able classes have it as base /// LuaObject, all script-able classes have it as base
// @bindclass // @bindclass
@ -76,10 +75,7 @@ public:
int getUseCount(); int getUseCount();
/// Returns the derived class name, its the same name used in Lua /// Returns the derived class name, its the same name used in Lua
std::string getClassName() { std::string getClassName();
// TODO: this could be cached for more performance
return stdext::demangle_name(typeid(*this).name());
}
LuaObjectPtr asLuaObject() { return static_self_cast<LuaObject>(); } LuaObjectPtr asLuaObject() { return static_self_cast<LuaObject>(); }
@ -87,7 +83,6 @@ public:
private: private:
int m_fieldsTableRef; int m_fieldsTableRef;
int m_metatableRef;
}; };
#include "luainterface.h" #include "luainterface.h"

View File

@ -25,6 +25,9 @@
#include <framework/luaengine/luainterface.h> #include <framework/luaengine/luainterface.h>
#include <otclient/otclient.h> #include <otclient/otclient.h>
#include "otclient/item.h"
#include "otclient/tile.h"
int main(int argc, const char* argv[]) int main(int argc, const char* argv[])
{ {
std::vector<std::string> args(argv, argv + argc); std::vector<std::string> args(argv, argv + argc);

View File

@ -127,7 +127,7 @@ private:
uint8 m_countOrSubType; uint8 m_countOrSubType;
stdext::packed_storage<uint8> m_attribs; stdext::packed_storage<uint8> m_attribs;
}; };
#pragma pack(pop) #pragma pack(pop)
#endif #endif