Skip to content

⚡ Perf: Optimize Raster Grid Generation (Speed & Memory)#27

Merged
e-kotov merged 4 commits intomainfrom
perf/raster-generation-speedups
Jan 16, 2026
Merged

⚡ Perf: Optimize Raster Grid Generation (Speed & Memory)#27
e-kotov merged 4 commits intomainfrom
perf/raster-generation-speedups

Conversation

@e-kotov
Copy link
Copy Markdown
Owner

@e-kotov e-kotov commented Jan 16, 2026

⚡ Performance Optimization: Raster Grid Generation

This PR implements significant performance speedups (~13,000x for inner loops) and memory usage reductions for raster grid generation, covering both sequential and parallel backends.

📝 Key Changes

1. `R/stream_grid_raster.R` (Sequential Streaming)

  • Optimization: Replaced redundant internal coordinate calculation loop.
  • Before: `cells -> terra::xyFromCell -> coords -> indices -> cell_ids`
  • After: `cells -> cell_ids` (Direct assignment)
  • Impact: ~13,000x speedup for the specific ID generation block.

2. `R/stream_grid_raster_parallel.R` (Parallel Streaming)

  • Optimization: Replaced expensive `rep()` based vector recycling with direct arithmetic sequence generation.
  • Impact: Eliminates allocation of large intermediate integer vectors for every processed chunk, reducing memory pressure and GC overhead.

3. `R/backend_sequential.R` (In-Memory/Sequential)

  • Optimization: Replaced `terra::xyFromCell(r, all_cells)` with efficient vectorized coordinate sequences.
  • Why: `terra::xyFromCell` generates a full coordinate matrix (N cells x 2) which is extremely memory intensive for large grids (e.g., 1.6GB for 100M cells). The new approach uses minimal memory (~KB) regardless of grid size.

✅ Verification

  • Correctness: Verified that the optimized coordinate/ID generation logic produces identical results to the original `terra` functions.
  • Tests: All raster-related tests passed (`devtools::test(filter="raster")`).

📊 Benchmark Results (Micro-benchmark)

Metric Original Optimized Improvement
Execution Time ~2.13 ms ~164 ns ~13,000x
Memory Alloc 8.39 MB 5.39 KB ~1,500x

Measured on a 100-row chunk of a 1000x1000 grid.

- R/stream_grid_raster.R: Replace round-trip definition of coordinates with direct cell ID assignment (~13,000x faster for the inner loop).
- R/stream_grid_raster_parallel.R: Replace vector recycling with direct sequence generation for chunks to reduce memory allocation.
- R/backend_sequential.R: Replace memory-intensive terra::xyFromCell with efficient vectorized logic for sequential processing.
Copilot AI review requested due to automatic review settings January 16, 2026 11:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes raster grid generation by eliminating unnecessary coordinate computations and replacing expensive memory operations with efficient direct calculations. The changes target three files covering sequential streaming, parallel streaming, and in-memory generation workflows.

Changes:

  • Replaces complex coordinate-to-index calculation loop with direct cell ID assignment in sequential streaming
  • Eliminates large intermediate vector allocations in parallel chunk processing by using direct sequence generation
  • Replaces memory-intensive terra::xyFromCell calls with efficient vectorized coordinate sequences in in-memory generation

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
R/stream_grid_raster.R Simplified cell ID generation by directly using terra's cell IDs instead of recomputing them from coordinates
R/stream_grid_raster_parallel.R Replaced inefficient rep()-based vector generation with direct integer sequence for contiguous cell ranges
R/backend_sequential.R Replaced terra::xyFromCell with efficient vectorized coordinate sequences to reduce memory footprint

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread R/backend_sequential.R
Comment thread R/stream_grid_raster_parallel.R Outdated
Copy link
Copy Markdown

Copilot AI commented Jan 16, 2026

@e-kotov I've opened a new pull request, #28, to work on those changes. Once the pull request is ready, I'll request review from you.

@e-kotov e-kotov merged commit 4b4ffef into main Jan 16, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants