Skip to content

Reduce allocations in grid pathfinding queues - #1263

Open
AlexisDrogoul wants to merge 1 commit into
mainfrom
alexisdrogoul-performance-improvements-8cb
Open

AlexisDrogoul wants to merge 1 commit into
mainfrom
alexisdrogoul-performance-improvements-8cb

Conversation

@AlexisDrogoul

Copy link
Copy Markdown
Member

Summary

Replace the anonymous ArrayList objects used as priority-queue entries in grid Dijkstra, A*, and JPS with a compact typed entry containing only the agent and its priority. This removes per-entry list/backing-array allocations and unchecked casts while preserving the existing priority values and ordering.

Other performance opportunities identified

  1. High impact / low effort — selected: compact priority-queue entries for grid pathfinding (GamaSpatialMatrix.java:1195-1349); avoids multiple allocations for every enqueued cell across three algorithms.
  2. High impact / low effort: avoid repeatedly reallocating and copying the accumulated neighborhood arrays as grid cache radii expand (GridNeighborhood.java:67-96).
  3. High impact / low effort: avoid building a full agent list solely to size the pathfinding open array (GamaSpatialMatrix.java:1143,1195,1266,1327).
  4. Medium-high impact / low effort: replace repeated linear edge membership checks with a set during graph-topology filtering (GraphTopology.java:1139).
  5. Medium-high impact / low effort: precompute vertex-to-index mappings instead of using List.indexOf for every edge while initializing Floyd-Warshall (FloydWarshallShortestPathsGAMA.java:156-157).
  6. Medium impact / low effort: reuse vertex indices for repeated shortest-path lookups instead of two linear indexOf scans per query (FloydWarshallShortestPathsGAMA.java:259-260, PathComputer.java matrix route lookup).
  7. Medium impact / low effort: avoid allocating a merged adjacency list for every expanded node in undirected bidirectional A* (NBAStarPathfinder.java:159,217).
  8. Medium impact / low effort: remove the fallback stream scan over all visited vertices in path reconstruction by resolving vertices with the existing identity/equality semantics (NBAStarPathfinder.java:319).
  9. Medium impact / low effort: use a membership set alongside the result list to avoid repeated linear contains checks during spatial splitting (SpatialTransformations.java:2410).
  10. Low impact / low effort: correct the undersized initial capacity for 8-neighbor rings to avoid repeated ArrayList growth (NoCacheNeighborhood.java:88).

Validation

mvn -f gama.parent/pom.xml -pl ../gama.core -am -DskipTests -Plocal-dev compile passed.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 05:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused refactor preserves queue behavior, with no unresolved findings.

No open issues 🎉

What changed in this PR

Reduces allocations in grid pathfinding by replacing list-based priority-queue entries with compact typed entries.

Changes:

  • Updates Dijkstra, A*, and JPS to use typed entries, removing unchecked casts.
  • Preserves priority calculations and ordering using Double.compare.
File Description
gama.core/​src/​gama/​core/​topology/​grid/​GamaSpatialMatrix.java Introduces compact queue entries and updates all three pathfinding algorithms.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants