Optimize lua object push

* Optimize position hash
* Fix minimap reload
This commit is contained in:
Eduardo Bart 2012-06-24 08:20:17 -03:00
parent b35063b022
commit eec6114b2d
5 changed files with 25 additions and 4 deletions

View File

@ -74,6 +74,7 @@ function Minimap.terminate()
disconnect(g_game, { onGameStart = Minimap.reset }) disconnect(g_game, { onGameStart = Minimap.reset })
Keyboard.unbindKeyDown('Ctrl+M') Keyboard.unbindKeyDown('Ctrl+M')
minimapButton:destroy()
minimapWindow:destroy() minimapWindow:destroy()
minimapWindow = nil minimapWindow = nil
minimapWidget = nil minimapWidget = nil

View File

@ -1052,8 +1052,7 @@ void LuaInterface::pushObject(const LuaObjectPtr& obj)
new(newUserdata(sizeof(LuaObjectPtr))) LuaObjectPtr(obj); new(newUserdata(sizeof(LuaObjectPtr))) LuaObjectPtr(obj);
m_totalObjRefs++; m_totalObjRefs++;
// set the userdata metatable obj->luaGetMetatable();
getGlobal(stdext::format("%s_mt", obj->getClassName()));
assert(!isNil()); assert(!isNil());
setMetatable(); setMetatable();
} }

View File

@ -26,7 +26,8 @@
#include <framework/application.h> #include <framework/application.h>
LuaObject::LuaObject() : LuaObject::LuaObject() :
m_fieldsTableRef(-1) m_fieldsTableRef(-1),
m_metatableRef(-1)
{ {
} }
@ -34,6 +35,11 @@ LuaObject::~LuaObject()
{ {
assert(!g_app.isTermianted()); assert(!g_app.isTermianted());
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)
@ -81,6 +87,17 @@ 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();
}
g_lua.getRef(m_metatableRef);
}
void LuaObject::luaGetFieldsTable() void LuaObject::luaGetFieldsTable()
{ {
if(m_fieldsTableRef != -1) if(m_fieldsTableRef != -1)

View File

@ -61,6 +61,9 @@ public:
/// Gets a field from this lua object, the result is pushed onto the stack /// Gets a field from this lua object, the result is pushed onto the stack
void luaGetField(const std::string& key); void luaGetField(const std::string& key);
/// Get object's metatable
void luaGetMetatable();
/// Gets the table containing all stored fields of this lua object, the result is pushed onto the stack /// Gets the table containing all stored fields of this lua object, the result is pushed onto the stack
void luaGetFieldsTable(); void luaGetFieldsTable();
@ -78,6 +81,7 @@ public:
private: private:
int m_fieldsTableRef; int m_fieldsTableRef;
int m_metatableRef;
}; };
#include "luainterface.h" #include "luainterface.h"

View File

@ -214,7 +214,7 @@ public:
struct PositionHasher : std::unary_function<Position, std::size_t> { struct PositionHasher : std::unary_function<Position, std::size_t> {
std::size_t operator()(const Position& pos) const { std::size_t operator()(const Position& pos) const {
return ((((pos.x * 32768) + pos.y) * 16) + pos.z) % 1000000; return ((((pos.x * 2048) + pos.y) * 16) + pos.z) % (2048*2048);
} }
}; };