Explorar o código

CPathfinder::addNeighbours: avoid allocating new vector each time

ArseniyShestakov %!s(int64=10) %!d(string=hai) anos
pai
achega
6dacb84404
Modificáronse 2 ficheiros con 6 adicións e 5 borrados
  1. 5 5
      lib/CPathfinder.cpp
  2. 1 0
      lib/CPathfinder.h

+ 5 - 5
lib/CPathfinder.cpp

@@ -53,6 +53,7 @@ CPathfinder::CPathfinder(CPathsInfo & _out, CGameState * _gs, const CGHeroInstan
 	hlp = make_unique<CPathfinderHelper>(hero, options);
 
 	initializeGraph();
+	neighbourTiles.reserve(8);
 	neighbours.reserve(16);
 }
 
@@ -192,19 +193,18 @@ void CPathfinder::calculatePaths()
 void CPathfinder::addNeighbours()
 {
 	neighbours.clear();
-	std::vector<int3> tiles;
-	tiles.reserve(8);
-	CPathfinderHelper::getNeighbours(gs->map, *ct, cp->coord, tiles, boost::logic::indeterminate, cp->layer == ELayer::SAIL);
+	neighbourTiles.clear();
+	CPathfinderHelper::getNeighbours(gs->map, *ct, cp->coord, neighbourTiles, boost::logic::indeterminate, cp->layer == ELayer::SAIL);
 	if(isSourceVisitableObj())
 	{
-		for(int3 tile: tiles)
+		for(int3 tile: neighbourTiles)
 		{
 			if(canMoveBetween(tile, ctObj->visitablePos()))
 				neighbours.push_back(tile);
 		}
 	}
 	else
-		vstd::concatenate(neighbours, tiles);
+		vstd::concatenate(neighbours, neighbourTiles);
 }
 
 void CPathfinder::addTeleportExits()

+ 1 - 0
lib/CPathfinder.h

@@ -169,6 +169,7 @@ private:
 	};
 	boost::heap::priority_queue<CGPathNode *, boost::heap::compare<NodeComparer> > pq;
 
+	std::vector<int3> neighbourTiles;
 	std::vector<int3> neighbours;
 
 	CGPathNode * cp; //current (source) path node -> we took it from the queue