Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations for NavMap synchronization #98866

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/navigation/3d/nav_mesh_queries_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
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];

// Only consider the connection to another polygon if this polygon is in a region with compatible layers.
Expand Down
9 changes: 2 additions & 7 deletions modules/navigation/nav_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,8 @@ void NavMap::sync() {

_new_pm_polygon_count = polygon_count;

struct ConnectionPair {
gd::Edge::Connection connections[2];
int size = 0;
};

// Group all edges per key.
HashMap<gd::EdgeKey, ConnectionPair, gd::EdgeKey> connection_pairs_map;
connection_pairs_map.clear();
connection_pairs_map.reserve(polygons.size());
int free_edges_count = 0; // How many ConnectionPairs have only one Connection.

Expand Down Expand Up @@ -469,7 +464,7 @@ void NavMap::sync() {
}
}

LocalVector<gd::Edge::Connection> free_edges;
free_edges.clear();
free_edges.reserve(free_edges_count);

for (const KeyValue<gd::EdgeKey, ConnectionPair> &pair_it : connection_pairs_map) {
Expand Down
8 changes: 8 additions & 0 deletions modules/navigation/nav_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ class NavMap : public NavRid {

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:
NavMap();
~NavMap();
Expand Down
2 changes: 1 addition & 1 deletion modules/navigation/nav_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct Edge {
};

/// Connections from this edge to other polygons.
Vector<Connection> connections;
LocalVector<Connection> connections;
};

struct Polygon {
Expand Down
Loading