Skip to content

⚡ Optimize inspire_id_to_coords multiplier calculation#25

Merged
e-kotov merged 1 commit intomainfrom
perf/optimize-inspire-id-to-coords
Jan 16, 2026
Merged

⚡ Optimize inspire_id_to_coords multiplier calculation#25
e-kotov merged 1 commit intomainfrom
perf/optimize-inspire-id-to-coords

Conversation

@e-kotov
Copy link
Copy Markdown
Owner

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

⚡ Optimize inspire_id_to_coords multiplier calculation

💡 What

Replaced the O(N) vapply loop for calculating coordinate multipliers with a vectorized approach using unique and match.

🎯 Why

The original code recalculated the multiplier (power of 10 based on trailing zeros) for every single ID. Since there are typically very few unique resolution values (e.g., '1km', '100m'), calculating the multiplier once per unique resolution and mapping it back avoids hundreds of thousands of redundant function calls.

📊 Measured Improvement

Benchmarks on 100,000 synthetic IDs show:

  • Isolated Logic: Reduced execution time for the multiplier calculation step from 0.152s to 0.002s (a ~60x speedup).
  • Overall Function: Reduced total execution time for inspire_id_to_coords from ~0.85s to ~0.68s (~20% total improvement).

All tests passed.

Copilot AI review requested due to automatic review settings January 16, 2026 09:11
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 pull request optimizes the multiplier calculation logic in the inspire_id_to_coords function by replacing an O(N) vapply loop with a vectorized approach using unique and match. The optimization focuses on avoiding redundant recalculations when multiple IDs share the same cellsize resolution.

Changes:

  • Replaced iterative multiplier calculation with a vectorized approach that calculates multipliers only once per unique cellsize value
  • Added USE.NAMES = FALSE parameter to vapply for consistency
  • Restructured the code to use unique and match for efficient mapping
Comments suppressed due to low confidence (2)

R/inspire_id_to_coords.R:1

  • The same optimization applied to inspire_id_to_coords.R should be applied to inspire_id_format.R at lines 88-90 (long-to-short conversion) and lines 132-134 (short-to-long conversion). These locations have the identical O(N) pattern that could benefit from the vectorized approach using unique and match.
#' @rdname inspire_id_coords

R/inspire_id_to_coords.R:1

  • The same optimization applied to inspire_id_to_coords.R should be applied to inspire_id_format.R at lines 88-90 (long-to-short conversion) and lines 132-134 (short-to-long conversion). These locations have the identical O(N) pattern that could benefit from the vectorized approach using unique and match.
#' @rdname inspire_id_coords

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

@e-kotov e-kotov merged commit 957cae9 into main Jan 16, 2026
17 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.

2 participants