-
Notifications
You must be signed in to change notification settings - Fork 3
Phase 2: Optimize gh_covering Performance (2-3× Speedup) #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dshkol
wants to merge
2
commits into
master
Choose a base branch
from
perf/gh-covering-direct-grid
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| # CI & codecov-related | ||
| ^\.travis\.yml$ | ||
| ^\.lintr$ | ||
| ^benchmarks$ | ||
|
|
||
| ^logo_maker.R$ | ||
| ^_pkgdown\.yml$ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Simple benchmark comparing old vs new gh_covering | ||
| # Loads both versions and compares directly | ||
|
|
||
| library(microbenchmark) | ||
| library(ggplot2) | ||
| library(sp) | ||
|
|
||
| # Define old version (before optimization) | ||
| gh_covering_old = function(SP, precision = 6L, minimal = FALSE) { | ||
| if (sf_input <- inherits(SP, 'sf')) { | ||
| SP = sf::as_Spatial(SP) | ||
| } | ||
| if (!inherits(SP, 'Spatial')) | ||
| stop('Object to cover must be Spatial (or subclass)') | ||
| if (inherits(SP, 'SpatialPointsDataFrame') && !NCOL(SP)) | ||
| SP$id = rownames(SP@data) | ||
| bb = sp::bbox(SP) | ||
| delta = 2.0 * geohashTools::gh_delta(precision) | ||
|
|
||
| # OLD: goes through encode-decode cycle | ||
| gh = with(expand.grid( | ||
| latitude = seq(bb[2L, 'min'], bb[2L, 'max'] + delta[1L], by = delta[1L]), | ||
| longitude = seq(bb[1L, 'min'], bb[1L, 'max'] + delta[2L], by = delta[2L]) | ||
| ), geohashTools::gh_encode(latitude, longitude, precision)) | ||
|
|
||
| wgs_crs = sp::CRS('+proj=longlat +datum=WGS84', doCheckCRSArgs = FALSE) | ||
| if (is.na(prj4 <- sp::proj4string(SP))) sp::proj4string(SP) = (prj4 <- wgs_crs) | ||
|
|
||
| # OLD: calls gh_to_sf which calls gh_to_spdf which calls gh_to_sp which decodes | ||
| cover = methods::as(geohashTools::gh_to_sf(gh), 'Spatial') | ||
| sp::proj4string(cover) = prj4 | ||
|
|
||
| if (minimal) { | ||
| n_in_cover = vapply(sp::over(cover, SP, returnList=TRUE), NROW, integer(1L)) | ||
| cover = cover[which(n_in_cover > 0L), ] | ||
| sp::proj4string(cover) = prj4 | ||
| } | ||
| return(if (sf_input) sf::st_as_sf(cover) else cover) | ||
| } | ||
|
|
||
| # Load new version | ||
| devtools::load_all(".", quiet = TRUE) | ||
|
|
||
| # Test data | ||
| set.seed(123) | ||
| test_points <- sp::SpatialPoints(cbind( | ||
| runif(100, 114.5, 115.0), | ||
| runif(100, -3.4, -3.2) | ||
| )) | ||
|
|
||
| cat("Benchmarking gh_covering: Old vs New\n") | ||
| cat("Test data: 100 points, 0.5 degree spread\n\n") | ||
|
|
||
| # Test different precisions | ||
| precisions <- c(4, 5, 6, 7, 8) | ||
| results <- list() | ||
|
|
||
| for (prec in precisions) { | ||
| cat(sprintf("Precision %d...\n", prec)) | ||
|
|
||
| bm <- microbenchmark( | ||
| old = gh_covering_old(test_points, precision = prec), | ||
| new = gh_covering(test_points, precision = prec), | ||
| times = 10L | ||
| ) | ||
|
|
||
| results[[as.character(prec)]] <- data.frame( | ||
| precision = prec, | ||
| method = c("old", "new"), | ||
| median_ms = c( | ||
| median(bm$time[bm$expr == "old"]) / 1e6, | ||
| median(bm$time[bm$expr == "new"]) / 1e6 | ||
| ) | ||
| ) | ||
|
|
||
| speedup <- results[[as.character(prec)]]$median_ms[1] / | ||
| results[[as.character(prec)]]$median_ms[2] | ||
| cat(sprintf(" Old: %.1f ms, New: %.1f ms, Speedup: %.2fx\n\n", | ||
| results[[as.character(prec)]]$median_ms[1], | ||
| results[[as.character(prec)]]$median_ms[2], | ||
| speedup)) | ||
| } | ||
|
|
||
| results_df <- do.call(rbind, results) | ||
| results_df$speedup <- with(results_df[results_df$method == "old", ], | ||
| median_ms) / results_df$median_ms[results_df$method == "new"] | ||
|
|
||
| # Create plot | ||
| p <- ggplot(results_df, aes(x = precision, y = median_ms, color = method)) + | ||
| geom_line(size = 1) + | ||
| geom_point(size = 3) + | ||
| scale_y_log10() + | ||
| labs( | ||
| title = "gh_covering Performance Comparison", | ||
| subtitle = "100 random points, 0.5° spread", | ||
| x = "Precision", | ||
| y = "Median Time (ms, log scale)", | ||
| color = "Method" | ||
| ) + | ||
| theme_minimal() + | ||
| theme(legend.position = "top") | ||
|
|
||
| ggsave("benchmarks/gh_covering_simple_comparison.png", p, width = 8, height = 6, dpi = 150) | ||
| cat("\nSaved plot to benchmarks/gh_covering_simple_comparison.png\n") | ||
|
|
||
| # Print summary | ||
| cat("\n=== Summary ===\n") | ||
| print(results_df) | ||
|
|
||
| cat(sprintf("\nMedian speedup across all precisions: %.2fx\n", | ||
| median(results_df$speedup[results_df$method == "new"]))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Benchmark script for gh_covering optimization | ||
| # Compares current implementation vs. optimized direct grid generation | ||
|
|
||
| # Load package from source | ||
| devtools::load_all(".", quiet = TRUE) | ||
|
|
||
| library(microbenchmark) | ||
| library(ggplot2) | ||
| library(sp) | ||
|
|
||
| # Create test data of varying sizes | ||
| create_test_points <- function(n_points = 100, spread = 1.0) { | ||
| sp::SpatialPoints(cbind( | ||
| runif(n_points, 114.5, 114.5 + spread), | ||
| runif(n_points, -3.4, -3.4 + spread) | ||
| )) | ||
| } | ||
|
|
||
| # Test different grid sizes by varying point spread and precision | ||
| test_cases <- expand.grid( | ||
| n_points = c(10, 100), | ||
| spread = c(0.1, 0.5, 1.0), # degrees - affects grid size | ||
| precision = c(4, 6, 8, 10), | ||
| stringsAsFactors = FALSE | ||
| ) | ||
|
|
||
| cat("Running benchmarks for gh_covering...\n") | ||
| cat("Test cases:", nrow(test_cases), "\n\n") | ||
|
|
||
| results <- vector("list", nrow(test_cases)) | ||
|
|
||
| for (i in seq_len(nrow(test_cases))) { | ||
| tc <- test_cases[i, ] | ||
| cat(sprintf("Test %d/%d: n_points=%d, spread=%.1f, precision=%d\n", | ||
| i, nrow(test_cases), tc$n_points, tc$spread, tc$precision)) | ||
|
|
||
| test_points <- create_test_points(tc$n_points, tc$spread) | ||
|
|
||
| # Run benchmark | ||
| bm <- microbenchmark( | ||
| current = gh_covering(test_points, precision = tc$precision), | ||
| times = 20L, | ||
| unit = "ms" | ||
| ) | ||
|
|
||
| results[[i]] <- data.frame( | ||
| n_points = tc$n_points, | ||
| spread = tc$spread, | ||
| precision = tc$precision, | ||
| median_ms = median(bm$time) / 1e6, | ||
| mean_ms = mean(bm$time) / 1e6, | ||
| min_ms = min(bm$time) / 1e6, | ||
| max_ms = max(bm$time) / 1e6 | ||
| ) | ||
|
|
||
| cat(sprintf(" Median: %.2f ms\n\n", results[[i]]$median_ms)) | ||
| } | ||
|
|
||
| results_df <- do.call(rbind, results) | ||
|
|
||
| # Save results | ||
| saveRDS(results_df, "benchmarks/gh_covering_baseline_results.rds") | ||
| cat("Saved baseline results to benchmarks/gh_covering_baseline_results.rds\n") | ||
|
|
||
| # Create visualization | ||
| p <- ggplot(results_df, aes(x = precision, y = median_ms, | ||
| color = factor(spread), | ||
| shape = factor(n_points))) + | ||
| geom_line() + | ||
| geom_point(size = 3) + | ||
| scale_y_log10() + | ||
| labs( | ||
| title = "gh_covering Performance (Baseline)", | ||
| subtitle = "Current encode-decode implementation", | ||
| x = "Precision", | ||
| y = "Median Time (ms, log scale)", | ||
| color = "Spread (degrees)", | ||
| shape = "Points" | ||
| ) + | ||
| theme_minimal() + | ||
| theme(legend.position = "right") | ||
|
|
||
| ggsave("benchmarks/gh_covering_baseline.png", p, width = 10, height = 6, dpi = 150) | ||
| cat("Saved plot to benchmarks/gh_covering_baseline.png\n") | ||
|
|
||
| # Print summary | ||
| cat("\n=== Summary Statistics ===\n") | ||
| print(results_df[order(results_df$median_ms, decreasing = TRUE), ]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # Comparison benchmark for gh_covering optimization | ||
| # Tests optimized version vs results from baseline | ||
|
|
||
| library(microbenchmark) | ||
| library(ggplot2) | ||
| library(sp) | ||
|
|
||
| # Load the optimized version | ||
| devtools::load_all(".", quiet = TRUE) | ||
|
|
||
| # Create test data | ||
| create_test_points <- function(n_points = 100, spread = 1.0) { | ||
| sp::SpatialPoints(cbind( | ||
| runif(n_points, 114.5, 114.5 + spread), | ||
| runif(n_points, -3.4, -3.4 + spread) | ||
| )) | ||
| } | ||
|
|
||
| # Smaller, faster test suite for comparison | ||
| test_cases <- expand.grid( | ||
| n_points = c(10, 100), | ||
| spread = c(0.1, 0.5, 1.0), | ||
| precision = c(4, 6, 8), | ||
| stringsAsFactors = FALSE | ||
| ) | ||
|
|
||
| cat("Running comparison benchmarks...\n") | ||
| cat("Testing optimized gh_covering implementation\n") | ||
| cat("Test cases:", nrow(test_cases), "\n\n") | ||
|
|
||
| results <- vector("list", nrow(test_cases)) | ||
|
|
||
| for (i in seq_len(nrow(test_cases))) { | ||
| tc <- test_cases[i, ] | ||
| cat(sprintf("Test %d/%d: n_points=%d, spread=%.1f, precision=%d\n", | ||
| i, nrow(test_cases), tc$n_points, tc$spread, tc$precision)) | ||
|
|
||
| test_points <- create_test_points(tc$n_points, tc$spread) | ||
|
|
||
| # Run benchmark on optimized version | ||
| bm <- microbenchmark( | ||
| optimized = gh_covering(test_points, precision = tc$precision), | ||
| times = 20L, | ||
| unit = "ms" | ||
| ) | ||
|
|
||
| results[[i]] <- data.frame( | ||
| n_points = tc$n_points, | ||
| spread = tc$spread, | ||
| precision = tc$precision, | ||
| median_ms = median(bm$time) / 1e6, | ||
| mean_ms = mean(bm$time) / 1e6, | ||
| min_ms = min(bm$time) / 1e6, | ||
| max_ms = max(bm$time) / 1e6 | ||
| ) | ||
|
|
||
| cat(sprintf(" Median: %.2f ms\n\n", results[[i]]$median_ms)) | ||
| } | ||
|
|
||
| optimized_df <- do.call(rbind, results) | ||
|
|
||
| # Save optimized results | ||
| saveRDS(optimized_df, "benchmarks/gh_covering_optimized_results.rds") | ||
| cat("\nSaved optimized results to benchmarks/gh_covering_optimized_results.rds\n") | ||
|
|
||
| # Load baseline if it exists and compare | ||
| if (file.exists("benchmarks/gh_covering_baseline_results.rds")) { | ||
| baseline_df <- readRDS("benchmarks/gh_covering_baseline_results.rds") | ||
|
|
||
| # Merge results | ||
| baseline_df$version <- "baseline" | ||
| optimized_df$version <- "optimized" | ||
| combined_df <- rbind(baseline_df[, names(optimized_df)], optimized_df) | ||
|
|
||
| # Calculate speedup | ||
| comparison <- merge( | ||
| baseline_df[, c("n_points", "spread", "precision", "median_ms")], | ||
| optimized_df[, c("n_points", "spread", "precision", "median_ms")], | ||
| by = c("n_points", "spread", "precision"), | ||
| suffixes = c("_baseline", "_optimized") | ||
| ) | ||
| comparison$speedup <- comparison$median_ms_baseline / comparison$median_ms_optimized | ||
|
|
||
| cat("\n=== Performance Comparison ===\n") | ||
| print(comparison[order(-comparison$speedup), ]) | ||
|
|
||
| cat(sprintf("\nMedian speedup: %.2fx\n", median(comparison$speedup))) | ||
| cat(sprintf("Mean speedup: %.2fx\n", mean(comparison$speedup))) | ||
| cat(sprintf("Max speedup: %.2fx\n", max(comparison$speedup))) | ||
|
|
||
| # Create comparison plot | ||
| p <- ggplot(combined_df, aes(x = precision, y = median_ms, | ||
| color = version, | ||
| linetype = factor(spread))) + | ||
| geom_line() + | ||
| geom_point(size = 2) + | ||
| facet_wrap(~n_points, labeller = label_both) + | ||
| scale_y_log10() + | ||
| labs( | ||
| title = "gh_covering Performance: Baseline vs Optimized", | ||
| x = "Precision", | ||
| y = "Median Time (ms, log scale)", | ||
| color = "Version", | ||
| linetype = "Spread (degrees)" | ||
| ) + | ||
| theme_minimal() + | ||
| theme(legend.position = "bottom") | ||
|
|
||
| ggsave("benchmarks/gh_covering_comparison.png", p, width = 12, height = 6, dpi = 150) | ||
| cat("\nSaved comparison plot to benchmarks/gh_covering_comparison.png\n") | ||
|
|
||
| # Speedup plot | ||
| p2 <- ggplot(comparison, aes(x = precision, y = speedup, | ||
| color = factor(spread), | ||
| shape = factor(n_points))) + | ||
| geom_line() + | ||
| geom_point(size = 3) + | ||
| geom_hline(yintercept = 1, linetype = "dashed", color = "gray50") + | ||
| labs( | ||
| title = "gh_covering Speedup: Optimized vs Baseline", | ||
| subtitle = "Values > 1.0 indicate optimized is faster", | ||
| x = "Precision", | ||
| y = "Speedup Factor", | ||
| color = "Spread (degrees)", | ||
| shape = "Points" | ||
| ) + | ||
| theme_minimal() + | ||
| theme(legend.position = "right") | ||
|
|
||
| ggsave("benchmarks/gh_covering_speedup.png", p2, width = 10, height = 6, dpi = 150) | ||
| cat("Saved speedup plot to benchmarks/gh_covering_speedup.png\n") | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to avoid non-ASCII in NEWS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, superfluous.