Optimize table usage in methods

This commit is contained in:
Rochet2
2017-03-19 16:27:46 +02:00
parent b7c379a42c
commit cc2037461c
8 changed files with 26 additions and 45 deletions

View File

@@ -779,26 +779,21 @@ namespace LuaCreature
*/
int GetAITargets(lua_State* L, Creature* creature)
{
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 i = 0;
#ifdef MANGOS
ThreatList const& threatlist = creature->GetThreatManager().getThreatList();
#else
ThreatList const& threatlist = creature->getThreatManager().getThreatList();
#endif
if (threatlist.empty())
return 1;
lua_createtable(L, threatlist.size(), 0);
int tbl = lua_gettop(L);
uint32 i = 0;
for (ThreatList::const_iterator itr = threatlist.begin(); itr != threatlist.end(); ++itr)
{
Unit* target = (*itr)->getTarget();
if (!target)
continue;
++i;
Eluna::Push(L, i);
Eluna::Push(L, target);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
lua_settop(L, tbl);

View File

@@ -329,7 +329,7 @@ namespace LuaQuery
}
}
lua_settable(L, tbl);
lua_rawset(L, tbl);
}
lua_settop(L, tbl);

View File

@@ -47,7 +47,7 @@ public:
lua_pushstring(E->L, methodTable->name);
lua_pushlightuserdata(E->L, (void*)methodTable);
lua_pushcclosure(E->L, thunk, 1);
lua_settable(E->L, -3);
lua_rawset(E->L, -3);
}
lua_remove(E->L, -1);
@@ -239,7 +239,7 @@ public:
lua_pushstring(E->L, methodTable->name);
lua_pushlightuserdata(E->L, (void*)methodTable);
lua_pushcclosure(E->L, CallMethod, 1);
lua_settable(E->L, -3);
lua_rawset(E->L, -3);
}
lua_pop(E->L, 1);

View File

@@ -179,10 +179,8 @@ namespace LuaGlobalFunctions
if ((team == TEAM_NEUTRAL || player->GetTeamId() == team) && (!onlyGM || player->IsGameMaster()))
#endif
{
++i;
Eluna::Push(L, i);
Eluna::Push(L, player);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
}
}
@@ -214,13 +212,13 @@ namespace LuaGlobalFunctions
uint32 team = Eluna::CHECKVAL<uint32>(L, 3, TEAM_NEUTRAL);
lua_newtable(L);
int tbl = lua_gettop(L);
uint32 i = 0;
Map* map = eMapMgr->FindMap(mapID, instanceID);
if (!map)
return 1;
int tbl = lua_gettop(L);
uint32 i = 0;
Map::PlayerList const& players = map->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
{
@@ -233,10 +231,8 @@ namespace LuaGlobalFunctions
continue;
if (player->GetSession() && (team >= TEAM_NEUTRAL || player->GetTeamId() == team))
{
++i;
Eluna::Push(L, i);
Eluna::Push(L, player);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
}

View File

@@ -184,10 +184,8 @@ namespace LuaGroup
if (!member || !member->GetSession())
continue;
++i;
Eluna::Push(L, i);
Eluna::Push(L, member);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
lua_settop(L, tbl); // push table to top of stack

View File

@@ -42,10 +42,8 @@ namespace LuaGuild
{
if (player->IsInWorld() && player->GetGuildId() == guild->GetId())
{
++i;
Eluna::Push(L, i);
Eluna::Push(L, player);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
}
}

View File

@@ -1201,15 +1201,14 @@ namespace LuaUnit
ElunaUtil::ObjectGUIDCheck guidCheck(unit->GET_GUID());
list.remove_if(guidCheck);
lua_newtable(L);
lua_createtable(L, list.size(), 0);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<Unit*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
lua_settop(L, tbl);
@@ -1239,15 +1238,14 @@ namespace LuaUnit
ElunaUtil::ObjectGUIDCheck guidCheck(unit->GET_GUID());
list.remove_if(guidCheck);
lua_newtable(L);
lua_createtable(L, list.size(), 0);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<Unit*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
lua_settop(L, tbl);

View File

@@ -280,15 +280,14 @@ namespace LuaWorldObject
obj->VisitNearbyObject(range, searcher);
#endif
lua_newtable(L);
lua_createtable(L, list.size(), 0);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<Player*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
lua_settop(L, tbl);
@@ -322,15 +321,14 @@ namespace LuaWorldObject
obj->VisitNearbyObject(range, searcher);
#endif
lua_newtable(L);
lua_createtable(L, list.size(), 0);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<Creature*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
lua_settop(L, tbl);
@@ -362,15 +360,14 @@ namespace LuaWorldObject
obj->VisitNearbyObject(range, searcher);
#endif
lua_newtable(L);
lua_createtable(L, list.size(), 0);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<GameObject*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
lua_settop(L, tbl);
@@ -447,15 +444,14 @@ namespace LuaWorldObject
obj->VisitNearbyObject(range, searcher);
#endif
lua_newtable(L);
lua_createtable(L, list.size(), 0);
int tbl = lua_gettop(L);
uint32 i = 0;
for (std::list<WorldObject*>::const_iterator it = list.begin(); it != list.end(); ++it)
{
Eluna::Push(L, ++i);
Eluna::Push(L, *it);
lua_settable(L, tbl);
lua_rawseti(L, tbl, ++i);
}
lua_settop(L, tbl);