Description
Cache repeated computations to avoid redundant calculations that occur multiple times within the same function or across related method calls.
Current Implementation Issues
R/MappedData.R - getAestheticsForGeom() (Lines 151-159)
gsub() and setdiff() operations computed twice
- Called within loop at line 166 and again at line 171
- String manipulation repeated unnecessarily
R/MappedData.R - setLimits() (Lines 464-469)
gsub() on "y" and "x" repeated multiple times
- Pattern replacement done in loop without caching
R/MappedDataTimeProfile.R - Multiple locations
- Lines 361-363:
listOfAesthetics[which(...)]$aesthetic computed twice
- Lines 393-396: Same pattern repeated in
setyLimits()
- Lines 318-321: Filtering operations in
dataForPlot() repeated in loop
R/plotYVsX.R - ggplot_build() calls (Lines 405-433)
ggplot_build() called and result analyzed multiple times
- Very expensive operation that should be cached
- Result can be reused for multiple analyses
R/plotYVsX.R - getCountsWithin() (Lines 692-707)
- Repeated
rlang::eval_tidy() calls
- Evaluation results could be cached
R/utilities.R - tolower() calls (Lines 59, 177-180)
tolower(dimension) computed multiple times
- Same transformation done repeatedly per function call
Suggested Implementation
- Store intermediate results: Save computed values in local variables
- Use R6 private fields: For class methods, cache expensive computations in private fields
- Implement memoization: Use function memoization for pure functions with repeated calls
- Hoist invariant computations: Move loop-invariant code outside loops
Example Refactoring
Before:
for (item in items) {
result <- expensiveComputation(data)
doSomething(result, item)
}
After:
cachedResult <- expensiveComputation(data)
for (item in items) {
doSomething(cachedResult, item)
}
Expected Benefits
- Reduced redundant computations
- Faster method execution
- Lower CPU usage
- Better performance for complex plots
Implementation Notes
Files to Modify
R/MappedData.R
R/MappedDataTimeProfile.R
R/plotYVsX.R
R/utilities.R
Testing
- Run existing unit tests
- Verify cached values are correct
- Test that cache invalidation works properly
- Add tests for edge cases
Description
Cache repeated computations to avoid redundant calculations that occur multiple times within the same function or across related method calls.
Current Implementation Issues
R/MappedData.R-getAestheticsForGeom()(Lines 151-159)gsub()andsetdiff()operations computed twiceR/MappedData.R-setLimits()(Lines 464-469)gsub()on "y" and "x" repeated multiple timesR/MappedDataTimeProfile.R- Multiple locationslistOfAesthetics[which(...)]$aestheticcomputed twicesetyLimits()dataForPlot()repeated in loopR/plotYVsX.R-ggplot_build()calls (Lines 405-433)ggplot_build()called and result analyzed multiple timesR/plotYVsX.R-getCountsWithin()(Lines 692-707)rlang::eval_tidy()callsR/utilities.R-tolower()calls (Lines 59, 177-180)tolower(dimension)computed multiple timesSuggested Implementation
Example Refactoring
Before:
After:
Expected Benefits
Implementation Notes
Files to Modify
R/MappedData.RR/MappedDataTimeProfile.RR/plotYVsX.RR/utilities.RTesting