Skip to content

add cpp backend for x2-3 speed up of vector grid generation#30

Open
e-kotov wants to merge 18 commits intomainfrom
faster
Open

add cpp backend for x2-3 speed up of vector grid generation#30
e-kotov wants to merge 18 commits intomainfrom
faster

Conversation

@e-kotov
Copy link
Copy Markdown
Owner

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

No description provided.

Copilot AI review requested due to automatic review settings January 16, 2026 17:33
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 adds a C++ backend for vector grid generation to achieve a 2-3x speed improvement. The implementation includes three new C++ functions for generating grid polygons, IDs, and parsing INSPIRE IDs, with a new vector_grid_backend parameter allowing users to choose between the optimized C++ backend (now the default) or the legacy R/sfheaders backend.

Changes:

  • Added C++ implementation for grid geometry generation, ID creation, and ID parsing
  • Introduced vector_grid_backend parameter to all grid generation functions with "cpp" as the default
  • Replaced R-based ID generation and parsing with C++ equivalents for performance
  • Added comprehensive test coverage comparing C++ and sfheaders backends

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/grid_worker.cpp Core C++ implementations for grid generation, ID generation, and ID parsing
src/RcppExports.cpp Auto-generated Rcpp exports for C++ functions
R/vector_backend_cpp.R R wrapper for C++ grid generation backend
R/backend_sequential.R Integration of C++ backend into sequential grid generation workflow
R/inspire_grid.R Added vector_grid_backend parameter to all inspire_grid methods
R/inspire_id_to_coords.R Replaced R parsing logic with C++ implementation
R/utils_as_grid.R Performance optimizations for sfheaders backend
tests/testthat/test-backend_cpp.R Comprehensive test comparing C++ and sfheaders backends
DESCRIPTION Added Rcpp dependency
NAMESPACE Exported new C++ functions

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

Comment on lines +12 to +14
# Fallback or try to parse 'EPSG:3035' string?
# For now default to 3035 as per original logic if missing
epsg <- 3035
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The hardcoded fallback to EPSG:3035 should be consistent with the behavior in other parts of the codebase and properly documented. Consider extracting this to a constant or helper function to avoid magic numbers.

Copilot uses AI. Check for mistakes.
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

Copilot reviewed 31 out of 31 changed files in this pull request and generated 9 comments.


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

NAMESPACE Outdated
S3method(inspire_grid,sf)
S3method(inspire_grid,sfc)
export(generate_ids_rcpp)
export(grid_worker_rcpp)
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The function convert_inspire_ids_rcpp is documented with @export in R/RcppExports.R (line 61) but is missing from the NAMESPACE file. This will prevent the function from being exported. Either add export(convert_inspire_ids_rcpp) to NAMESPACE or remove the @export tag from the documentation if the function should remain internal.

Suggested change
export(grid_worker_rcpp)
export(grid_worker_rcpp)
export(convert_inspire_ids_rcpp)

Copilot uses AI. Check for mistakes.
NAMESPACE Outdated
export(inspire_id_format)
export(inspire_id_from_coords)
export(inspire_id_to_coords)
export(parse_inspire_ids_rcpp)
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The function convert_inspire_ids_rcpp is documented with @export in R/RcppExports.R (line 61) but is missing from the NAMESPACE file. This will prevent the function from being exported. Either add export(convert_inspire_ids_rcpp) to NAMESPACE or remove the @export tag from the documentation if the function should remain internal.

Suggested change
export(parse_inspire_ids_rcpp)
export(parse_inspire_ids_rcpp)
export(convert_inspire_ids_rcpp)

Copilot uses AI. Check for mistakes.
Comment on lines +323 to +325
nzeros <- .tz_count(cellsize_m)
div <- 10^nzeros
size_lbl <- if (cellsize_m >= 1000) paste0(cellsize_m / 1000, "km") else paste0(cellsize_m, "m")
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

This code block for calculating nzeros, div, and size_lbl is duplicated in at least three places in this file (lines 207-209, 323-325, and 388-390). Consider extracting this into a helper function to improve maintainability and reduce duplication.

Copilot uses AI. Check for mistakes.
)
}


Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

Extra blank line on line 39 should be removed for consistency.

Suggested change

Copilot uses AI. Check for mistakes.
Comment on lines +64 to 65


Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

Multiple consecutive blank lines (63-64) should be reduced to a single blank line for consistency.

Suggested change

Copilot uses AI. Check for mistakes.
Comment on lines 117 to 120
})


test_that("inspire_id_to_coords warns on multiple CRSs", {
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

Extra blank line on line 119 should be removed for consistency.

Copilot uses AI. Check for mistakes.

sprintf("CRS%.0fRES%.0fmN%.0fE%.0f", crs, cellsize_m, y_long, x_long)
}
# 3. Perform conversion based on format
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

Duplicate comment on line 72. The comment '# 3. Perform conversion based on format' appears both before and after the match.arg call, creating redundancy.

Suggested change
# 3. Perform conversion based on format

Copilot uses AI. Check for mistakes.
Comment on lines +72 to +75
dsn = dsn,
layer = layer,
quiet = quiet,
vector_grid_backend = vector_grid_backend
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

Inconsistent indentation: lines 72-74 use extra spaces compared to line 75 (vector_grid_backend = vector_grid_backend). All parameters should be aligned consistently.

Suggested change
dsn = dsn,
layer = layer,
quiet = quiet,
vector_grid_backend = vector_grid_backend
dsn = dsn,
layer = layer,
quiet = quiet,
vector_grid_backend = vector_grid_backend

Copilot uses AI. Check for mistakes.
y = strtod(y_str.c_str(), NULL);

// Parse X (after E)
x = strtod(e_pos + 1, &end);
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

In the long-to-short conversion, when parsing the X coordinate after 'E', the end pointer is updated but not validated. Consider adding a check for trailing characters after parsing X to ensure complete consumption of the input string, similar to the validation done for short-to-long conversion (line 476) and the parse function (lines 274, 316).

Copilot uses AI. Check for mistakes.
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.

2 participants