From cc2037461ccf0b1b94ae0e2bf4585984c4b4bbb6 Mon Sep 17 00:00:00 2001 From: Rochet2 Date: Sun, 19 Mar 2017 16:27:46 +0200 Subject: [PATCH] Optimize table usage in methods --- CreatureMethods.h | 13 ++++--------- ElunaQueryMethods.h | 2 +- ElunaTemplate.h | 4 ++-- GlobalMethods.h | 14 +++++--------- GroupMethods.h | 4 +--- GuildMethods.h | 4 +--- UnitMethods.h | 10 ++++------ WorldObjectMethods.h | 20 ++++++++------------ 8 files changed, 26 insertions(+), 45 deletions(-) diff --git a/CreatureMethods.h b/CreatureMethods.h index 8181b92..a13c037 100644 --- a/CreatureMethods.h +++ b/CreatureMethods.h @@ -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); diff --git a/ElunaQueryMethods.h b/ElunaQueryMethods.h index 4f2ea38..32cdb23 100644 --- a/ElunaQueryMethods.h +++ b/ElunaQueryMethods.h @@ -329,7 +329,7 @@ namespace LuaQuery } } - lua_settable(L, tbl); + lua_rawset(L, tbl); } lua_settop(L, tbl); diff --git a/ElunaTemplate.h b/ElunaTemplate.h index b96a042..d0bb6b8 100644 --- a/ElunaTemplate.h +++ b/ElunaTemplate.h @@ -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); diff --git a/GlobalMethods.h b/GlobalMethods.h index d828efd..1bce4d4 100644 --- a/GlobalMethods.h +++ b/GlobalMethods.h @@ -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(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); } } diff --git a/GroupMethods.h b/GroupMethods.h index 3f76a73..779d29e 100644 --- a/GroupMethods.h +++ b/GroupMethods.h @@ -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 diff --git a/GuildMethods.h b/GuildMethods.h index 6e76422..be9f2e1 100644 --- a/GuildMethods.h +++ b/GuildMethods.h @@ -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); } } } diff --git a/UnitMethods.h b/UnitMethods.h index b3bee5b..34365c3 100644 --- a/UnitMethods.h +++ b/UnitMethods.h @@ -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::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::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); diff --git a/WorldObjectMethods.h b/WorldObjectMethods.h index 01a4fbc..e2faa25 100644 --- a/WorldObjectMethods.h +++ b/WorldObjectMethods.h @@ -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::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::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::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::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);