From a14f8058dae53d2f1dc4f0b83dbe146a38f3666b Mon Sep 17 00:00:00 2001 From: "Michael Cook (mackal)" Date: Wed, 17 Dec 2014 01:52:00 -0500 Subject: [PATCH] Switch path stuff to vectors This should be more CPU cache friendly compared to lists so should be faster. (profiling looked promising) --- changelog.txt | 3 +++ zone/client_packet.cpp | 4 +-- zone/entity.cpp | 2 +- zone/fearpath.cpp | 2 +- zone/mob.h | 2 +- zone/pathing.cpp | 58 ++++++++++++++++++++---------------------- zone/pathing.h | 6 ++--- 7 files changed, 39 insertions(+), 38 deletions(-) diff --git a/changelog.txt b/changelog.txt index 1a0be1590..d1184037d 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,8 @@ EQEMu Changelog (Started on Sept 24, 2003 15:50) ------------------------------------------------------- +== 12/17/2014 == +demonstar55: Use vectors for route stuff, should be more CPU cache friendly so faster + == 12/15/2014 == Trevius: (RoF+) Implemented the 6th Augment Slot for Items. Trevius: Player Corpses now saved attuned settings for Items. diff --git a/zone/client_packet.cpp b/zone/client_packet.cpp index be7d559e5..82147ca43 100644 --- a/zone/client_packet.cpp +++ b/zone/client_packet.cpp @@ -5714,7 +5714,7 @@ void Client::Handle_OP_FindPersonRequest(const EQApplicationPacket *app) } else { - std::list pathlist = zone->pathing->FindRoute(Start, End); + std::vector pathlist = zone->pathing->FindRoute(Start, End); if (pathlist.size() == 0) { @@ -5753,7 +5753,7 @@ void Client::Handle_OP_FindPersonRequest(const EQApplicationPacket *app) p.z = GetZ(); points.push_back(p); - for (std::list::iterator Iterator = pathlist.begin(); Iterator != pathlist.end(); ++Iterator) + for (auto Iterator = pathlist.begin(); Iterator != pathlist.end(); ++Iterator) { if ((*Iterator) == -1) // Teleporter { diff --git a/zone/entity.cpp b/zone/entity.cpp index 369e1503a..c7a980f3d 100644 --- a/zone/entity.cpp +++ b/zone/entity.cpp @@ -2636,7 +2636,7 @@ void EntityList::FindPathsToAllNPCs() while (it != npc_list.end()) { Map::Vertex Node0 = zone->pathing->GetPathNodeCoordinates(0, false); Map::Vertex Dest(it->second->GetX(), it->second->GetY(), it->second->GetZ()); - std::list Route = zone->pathing->FindRoute(Node0, Dest); + std::vector Route = zone->pathing->FindRoute(Node0, Dest); if (Route.size() == 0) printf("Unable to find a route to %s\n", it->second->GetName()); else diff --git a/zone/fearpath.cpp b/zone/fearpath.cpp index 89b34c444..2e8e146aa 100644 --- a/zone/fearpath.cpp +++ b/zone/fearpath.cpp @@ -158,7 +158,7 @@ void Mob::CalculateNewFearpoint() Map::Vertex CurrentPosition(GetX(), GetY(), GetZ()); - std::list Route = zone->pathing->FindRoute(CurrentPosition, Loc); + std::vector Route = zone->pathing->FindRoute(CurrentPosition, Loc); if(Route.size() > 0) { diff --git a/zone/mob.h b/zone/mob.h index e0a969fc4..cb7b82948 100644 --- a/zone/mob.h +++ b/zone/mob.h @@ -1225,7 +1225,7 @@ protected: Map::Vertex PathingLastPosition; int PathingLoopCount; int PathingLastNodeVisited; - std::list Route; + std::vector Route; LOSType PathingLOSState; Timer *PathingLOSCheckTimer; Timer *PathingRouteUpdateTimerShort; diff --git a/zone/pathing.cpp b/zone/pathing.cpp index 5501ad2ca..309d6c0ed 100644 --- a/zone/pathing.cpp +++ b/zone/pathing.cpp @@ -205,15 +205,15 @@ Map::Vertex PathManager::GetPathNodeCoordinates(int NodeNumber, bool BestZ) } -std::list PathManager::FindRoute(int startID, int endID) +std::vector PathManager::FindRoute(int startID, int endID) { _log(PATHING__DEBUG, "FindRoute from node %i to %i", startID, endID); memset(ClosedListFlag, 0, sizeof(int) * Head.PathNodeCount); - std::list OpenList, ClosedList; + std::vector OpenList, ClosedList; - std::listRoute; + std::vectorRoute; AStarNode AStarEntry, CurrentNode; @@ -235,7 +235,7 @@ std::list PathManager::FindRoute(int startID, int endID) ClosedListFlag[CurrentNode.PathNodeID] = true; - OpenList.pop_front(); + OpenList.erase(OpenList.begin()); for(int i = 0; i < PATHNODENEIGHBOURS; ++i) { @@ -251,7 +251,7 @@ std::list PathManager::FindRoute(int startID, int endID) Route.push_back(endID); - std::list::iterator RouteIterator; + std::vector::iterator RouteIterator; while(CurrentNode.PathNodeID != startID) { @@ -300,7 +300,7 @@ std::list PathManager::FindRoute(int startID, int endID) bool AlreadyInOpenList = false; - std::list::iterator OpenListIterator, InsertionPoint = OpenList.end(); + std::vector::iterator OpenListIterator, InsertionPoint = OpenList.end(); for(OpenListIterator = OpenList.begin(); OpenListIterator != OpenList.end(); ++OpenListIterator) { @@ -350,11 +350,11 @@ bool SortPathNodesByDistance(PathNodeSortStruct n1, PathNodeSortStruct n2) return n1.Distance < n2.Distance; } -std::list PathManager::FindRoute(Map::Vertex Start, Map::Vertex End) +std::vector PathManager::FindRoute(Map::Vertex Start, Map::Vertex End) { _log(PATHING__DEBUG, "FindRoute(%8.3f, %8.3f, %8.3f, %8.3f, %8.3f, %8.3f)", Start.x, Start.y, Start.z, End.x, End.y, End.z); - std::list noderoute; + std::vector noderoute; float CandidateNodeRangeXY = RuleR(Pathing, CandidateNodeRangeXY); @@ -365,7 +365,7 @@ std::list PathManager::FindRoute(Map::Vertex Start, Map::Vertex End) // int ClosestPathNodeToStart = -1; - std::list SortedByDistance; + std::vector SortedByDistance; PathNodeSortStruct TempNode; @@ -382,9 +382,9 @@ std::list PathManager::FindRoute(Map::Vertex Start, Map::Vertex End) } } - SortedByDistance.sort(SortPathNodesByDistance); + std::sort(SortedByDistance.begin(), SortedByDistance.end(), SortPathNodesByDistance); - for(std::list::iterator Iterator = SortedByDistance.begin(); Iterator != SortedByDistance.end(); ++Iterator) + for(auto Iterator = SortedByDistance.begin(); Iterator != SortedByDistance.end(); ++Iterator) { _log(PATHING__DEBUG, "Checking Reachability of Node %i from Start Position.", PathNodes[(*Iterator).id].id); @@ -420,9 +420,9 @@ std::list PathManager::FindRoute(Map::Vertex Start, Map::Vertex End) } } - SortedByDistance.sort(SortPathNodesByDistance); + std::sort(SortedByDistance.begin(), SortedByDistance.end(), SortPathNodesByDistance); - for(std::list::iterator Iterator = SortedByDistance.begin(); Iterator != SortedByDistance.end(); ++Iterator) + for(auto Iterator = SortedByDistance.begin(); Iterator != SortedByDistance.end(); ++Iterator) { _log(PATHING__DEBUG, "Checking Reachability of Node %i from End Position.", PathNodes[(*Iterator).id].id); _log(PATHING__DEBUG, " (%8.3f, %8.3f, %8.3f) to (%8.3f, %8.3f, %8.3f)", @@ -456,7 +456,7 @@ std::list PathManager::FindRoute(Map::Vertex Start, Map::Vertex End) { int CulledNodes = 0; - std::list::iterator First, Second; + std::vector::iterator First, Second; while((noderoute.size() >= 2) && (CulledNodes < NodesToAttemptToCull)) { @@ -487,7 +487,7 @@ std::list PathManager::FindRoute(Map::Vertex Start, Map::Vertex End) { int CulledNodes = 0; - std::list::iterator First, Second; + std::vector::iterator First, Second; while((noderoute.size() >= 2) && (CulledNodes < NodesToAttemptToCull)) { @@ -611,7 +611,7 @@ void PathManager::MeshTest() if(j == i) continue; - std::list Route = FindRoute(PathNodes[i].id, PathNodes[j].id); + std::vector Route = FindRoute(PathNodes[i].id, PathNodes[j].id); if(Route.size() == 0) { @@ -638,7 +638,7 @@ void PathManager::SimpleMeshTest() for(uint32 j = 1; j < Head.PathNodeCount; ++j) { - std::list Route = FindRoute(PathNodes[0].id, PathNodes[j].id); + std::vector Route = FindRoute(PathNodes[0].id, PathNodes[j].id); if(Route.size() == 0) { @@ -695,7 +695,7 @@ Map::Vertex Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool & } NodeLoc = zone->pathing->GetPathNodeCoordinates(Route.front()); - Route.pop_front(); + Route.erase(Route.begin()); ++PathingTraversedNodes; @@ -784,7 +784,7 @@ Map::Vertex Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool & } // We are on the same route, no LOS (or not checking this time, so pop off the node we just reached // - Route.pop_front(); + Route.erase(Route.begin()); ++PathingTraversedNodes; @@ -798,7 +798,7 @@ Map::Vertex Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool & if(NextNode == -1) { // -1 indicates a teleport to the next node - Route.pop_front(); + Route.erase(Route.begin()); if(Route.size() == 0) { @@ -814,7 +814,7 @@ Map::Vertex Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool & mlog(PATHING__DEBUG, " TELEPORTED to %8.3f, %8.3f, %8.3f\n", NodeLoc.x, NodeLoc.y, NodeLoc.z); - Route.pop_front(); + Route.erase(Route.begin()); if(Route.size() == 0) return To; @@ -962,7 +962,7 @@ Map::Vertex Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool & PathingLastNodeVisited = Route.front(); - Route.pop_front(); + Route.erase(Route.begin()); ++PathingTraversedNodes; @@ -975,7 +975,7 @@ Map::Vertex Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool & if(NextNode == -1) { // -1 indicates a teleport to the next node - Route.pop_front(); + Route.erase(Route.begin()); if(Route.size() == 0) { @@ -991,7 +991,7 @@ Map::Vertex Mob::UpdatePath(float ToX, float ToY, float ToZ, float Speed, bool & mlog(PATHING__DEBUG, " TELEPORTED to %8.3f, %8.3f, %8.3f\n", NodeLoc.x, NodeLoc.y, NodeLoc.z); - Route.pop_front(); + Route.erase(Route.begin()); if(Route.size() == 0) return To; @@ -1103,7 +1103,7 @@ int PathManager::FindNearestPathNode(Map::Vertex Position) int ClosestPathNodeToStart = -1; - std::list SortedByDistance; + std::vector SortedByDistance; PathNodeSortStruct TempNode; @@ -1120,9 +1120,9 @@ int PathManager::FindNearestPathNode(Map::Vertex Position) } } - SortedByDistance.sort(SortPathNodesByDistance); + std::sort(SortedByDistance.begin(), SortedByDistance.end(), SortPathNodesByDistance); - for(std::list::iterator Iterator = SortedByDistance.begin(); Iterator != SortedByDistance.end(); ++Iterator) + for(auto Iterator = SortedByDistance.begin(); Iterator != SortedByDistance.end(); ++Iterator) { _log(PATHING__DEBUG, "Checking Reachability of Node %i from Start Position.", PathNodes[(*Iterator).id].id); @@ -1262,9 +1262,7 @@ void Mob::PrintRoute() printf("Route is : "); - std::list::iterator Iterator; - - for(Iterator = Route.begin(); Iterator !=Route.end(); ++Iterator) + for(auto Iterator = Route.begin(); Iterator !=Route.end(); ++Iterator) { printf("%i, ", (*Iterator)); } diff --git a/zone/pathing.h b/zone/pathing.h index c01cb6853..8c131dbc3 100644 --- a/zone/pathing.h +++ b/zone/pathing.h @@ -3,7 +3,7 @@ #include "map.h" -#include +#include class Client; class Mob; @@ -60,8 +60,8 @@ public: static PathManager *LoadPathFile(const char *ZoneName); bool loadPaths(FILE *fp); void PrintPathing(); - std::list FindRoute(Map::Vertex Start, Map::Vertex End); - std::list FindRoute(int startID, int endID); + std::vector FindRoute(Map::Vertex Start, Map::Vertex End); + std::vector FindRoute(int startID, int endID); Map::Vertex GetPathNodeCoordinates(int NodeNumber, bool BestZ = true); bool CheckLosFN(Map::Vertex a, Map::Vertex b);