Skip to content

Conversation

@mountainMath
Copy link
Owner

Summary

Optimize get_cansim_table_template() by replacing iterative full_join operations with a single tidyr::expand_grid() call.

Before (iterative approach)

result <- tibble(...link="link", COORDINATE="")
for (i in seq_len(nrow(dimensions))) {
  result <- result %>%
    full_join(member, by="...link", relationship="many-to-many") %>%
    mutate(COORDINATE=ifelse(COORDINATE=="", memberId, paste0(COORDINATE, ".", memberId)))
}

After (vectorized approach)

result <- do.call(tidyr::expand_grid, dim_list)
coord_cols <- lapply(member_id_cols, function(col) result[[col]])
result$COORDINATE <- do.call(paste, c(coord_cols, sep="."))

Benchmark Results

Tested with installed package versions (mean of 5 runs each):

Table Rows Original Optimized Speedup
34-10-0013 50 0.0148s 0.0084s 43%
20-10-0001 660 0.0270s 0.0152s 44%
36-10-0402 13,143 0.0266s 0.0168s 37%
14-10-0287 32,076 0.0482s 0.0294s 39%
13-10-0096 66,528 0.0500s 0.0398s 20%

Correctness Verification

Output verified identical to original on all test tables:

  • Column names match: ✓
  • Row counts match: ✓
  • Data identical (when sorted by COORDINATE): ✓
Table 34-10-0013: Columns identical: TRUE, Data identical (sorted): TRUE
Table 20-10-0001: Columns identical: TRUE, Data identical (sorted): TRUE
Table 14-10-0287: Columns identical: TRUE, Data identical (sorted): TRUE

Test Plan

  • All existing tests pass (devtools::test() - 20 tests)
  • Output verified identical on multiple tables
  • Benchmarked with installed package versions

🤖 Generated with Claude Code

Replace iterative full_join with tidyr::expand_grid for Cartesian product:
- Old: Sequential full_join operations, updating COORDINATE after each join
- New: Single expand_grid call, vectorized paste for COORDINATE

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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