Skip to content

Avoid repeated full-band copies when building raster grids - #1256

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

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

Conversation

@AlexisDrogoul

Copy link
Copy Markdown
Member

Summary

Cache each raster band once while constructing a multi-band GamaSpatialMatrix. GamaGridFile.getBand() copies the full band array, so retrieving it inside the per-cell loop made initialization copy each band once per cell (O(bands × cells²)). The new code performs one copy per band and reuses those arrays while populating cells (O(bands × cells)).

Ten performance opportunities identified

  1. Implemented — Multi-band raster grid construction: avoid copying each full band for every cell in GamaSpatialMatrix.
  2. Multi-raster grid construction: investigate bulk sampling/reprojection instead of one valueOf and coordinate transform per raster per cell.
  3. Vectorized field diffusion: reuse or eliminate temporary lane arrays allocated inside fastDiffusionWithConvolution's vector loop.
  4. Masked convolution diffusion: avoid allocating an ArrayList per output cell and coordinate arrays per contributing neighbor.
  5. Masked dot-product diffusion: apply the same allocation reduction to diffusionWithDotProduct.
  6. Cached grid neighborhoods: grow neighbor buffers rather than copying all accumulated neighbor indices on each radius expansion.
  7. Uncached grid neighborhoods: reduce boxed Integer/ArrayList allocation per radius and correct the undersized perimeter list capacity.
  8. Grid closest-agent search: track visited cells in a reusable index-based structure rather than rebuilding visited/current-agent sets for each expansion.
  9. Grid pathfinding: replace anonymous ArrayList queue entries and boxed costs with a typed priority-queue entry.
  10. Quadtree nearest-N queries: compute candidate distances once rather than recalculating them in the comparator during selection.

The other items are source-level candidates for follow-up; they were not benchmarked as part of this change.

Validation

gama.core compiled successfully in the full Maven reactor. The full reactor later failed while resolving gama.feature.extensions because the local gama.dev p2 unit version was unavailable. git diff --check passed.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 02:08

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 optimization preserves existing indexing behavior while removing the repeated band-copy overhead.

0 open findings

What changed in this PR

Caches raster bands once during multi-band grid construction, eliminating repeated full-band copies.

Changes:

  • Loads each band once before iterating over cells.
  • Reuses cached arrays when populating per-cell band values.
File Description
gama.core/​src/​gama/​core/​topology/​grid/​GamaSpatialMatrix.java Optimizes multi-band raster grid initialization.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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