瀏覽代碼

Reduce allocations for NavMap synchronisation

Improves navigation map sync performance be avoiding unnecessary memory allocations.
smix8 10 月之前
父節點
當前提交
b840c9837a

+ 1 - 1
modules/navigation/3d/nav_mesh_queries_3d.cpp

@@ -234,7 +234,7 @@ Vector<Vector3> NavMeshQueries3D::polygons_get_path(const LocalVector<gd::Polygo
 		// Takes the current least_cost_poly neighbors (iterating over its edges) and compute the traveled_distance.
 		// Takes the current least_cost_poly neighbors (iterating over its edges) and compute the traveled_distance.
 		for (const gd::Edge &edge : navigation_polys[least_cost_id].poly->edges) {
 		for (const gd::Edge &edge : navigation_polys[least_cost_id].poly->edges) {
 			// Iterate over connections in this edge, then compute the new optimized travel distance assigned to this polygon.
 			// Iterate over connections in this edge, then compute the new optimized travel distance assigned to this polygon.
-			for (int connection_index = 0; connection_index < edge.connections.size(); connection_index++) {
+			for (uint32_t connection_index = 0; connection_index < edge.connections.size(); connection_index++) {
 				const gd::Edge::Connection &connection = edge.connections[connection_index];
 				const gd::Edge::Connection &connection = edge.connections[connection_index];
 
 
 				// Only consider the connection to another polygon if this polygon is in a region with compatible layers.
 				// Only consider the connection to another polygon if this polygon is in a region with compatible layers.

+ 2 - 7
modules/navigation/nav_map.cpp

@@ -426,13 +426,8 @@ void NavMap::sync() {
 
 
 		_new_pm_polygon_count = polygon_count;
 		_new_pm_polygon_count = polygon_count;
 
 
-		struct ConnectionPair {
-			gd::Edge::Connection connections[2];
-			int size = 0;
-		};
-
 		// Group all edges per key.
 		// Group all edges per key.
-		HashMap<gd::EdgeKey, ConnectionPair, gd::EdgeKey> connection_pairs_map;
+		connection_pairs_map.clear();
 		connection_pairs_map.reserve(polygons.size());
 		connection_pairs_map.reserve(polygons.size());
 		int free_edges_count = 0; // How many ConnectionPairs have only one Connection.
 		int free_edges_count = 0; // How many ConnectionPairs have only one Connection.
 
 
@@ -469,7 +464,7 @@ void NavMap::sync() {
 			}
 			}
 		}
 		}
 
 
-		LocalVector<gd::Edge::Connection> free_edges;
+		free_edges.clear();
 		free_edges.reserve(free_edges_count);
 		free_edges.reserve(free_edges_count);
 
 
 		for (const KeyValue<gd::EdgeKey, ConnectionPair> &pair_it : connection_pairs_map) {
 		for (const KeyValue<gd::EdgeKey, ConnectionPair> &pair_it : connection_pairs_map) {

+ 8 - 0
modules/navigation/nav_map.h

@@ -128,6 +128,14 @@ class NavMap : public NavRid {
 
 
 	HashMap<NavRegion *, LocalVector<gd::Edge::Connection>> region_external_connections;
 	HashMap<NavRegion *, LocalVector<gd::Edge::Connection>> region_external_connections;
 
 
+	struct ConnectionPair {
+		gd::Edge::Connection connections[2];
+		int size = 0;
+	};
+
+	HashMap<gd::EdgeKey, ConnectionPair, gd::EdgeKey> connection_pairs_map;
+	LocalVector<gd::Edge::Connection> free_edges;
+
 public:
 public:
 	NavMap();
 	NavMap();
 	~NavMap();
 	~NavMap();

+ 1 - 1
modules/navigation/nav_utils.h

@@ -94,7 +94,7 @@ struct Edge {
 	};
 	};
 
 
 	/// Connections from this edge to other polygons.
 	/// Connections from this edge to other polygons.
-	Vector<Connection> connections;
+	LocalVector<Connection> connections;
 };
 };
 
 
 struct Polygon {
 struct Polygon {