Skip to content

Commit a88c689

Browse files
authored
Merge pull request #26 from e-kotov/perf/optimize-raster-chunk-allocation
⚡ Optimize Parallel Raster Chunk Allocation
2 parents 957cae9 + 9f39250 commit a88c689

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

R/stream_grid_raster_parallel.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ NULL
4242
#' @noRd
4343
.compute_raster_chunk <- function(start_row, nrows, ncols) {
4444
# Generate row/col indices for the chunk
45-
rows <- rep(start_row:(start_row + nrows - 1), each = ncols)
46-
cols <- rep(1:ncols, times = nrows)
47-
48-
# Compute linear cell IDs (row-major order)
49-
cell_ids <- (rows - 1) * ncols + cols
45+
# Generate continuous sequence of cell IDs directly (row-major order)
46+
# Optimization: Avoid allocating large row/col vectors
47+
start_id <- (start_row - 1) * ncols + 1
48+
end_id <- start_id + (nrows * ncols) - 1
49+
cell_ids <- start_id:end_id
5050

5151
list(values = as.integer(cell_ids), start = start_row, nrows = nrows)
5252
}
@@ -249,9 +249,11 @@ stream_raster_parallel_mirai <- function(
249249
nrows_chunk <- chunk_def$nrows
250250
ncols_chunk <- chunk_def$ncols
251251

252-
rows <- rep(start_row:(start_row + nrows_chunk - 1), each = ncols_chunk)
253-
cols <- rep(1:ncols_chunk, times = nrows_chunk)
254-
cell_ids <- (rows - 1) * ncols_chunk + cols
252+
# Generate continuous sequence of cell IDs directly (row-major order)
253+
# Optimization: Avoid allocating large row/col vectors
254+
start_id <- (start_row - 1) * ncols_chunk + 1
255+
end_id <- start_id + (nrows_chunk * ncols_chunk) - 1
256+
cell_ids <- start_id:end_id
255257

256258
list(
257259
chunk_idx = chunk_idx,

0 commit comments

Comments
 (0)