Skip to content

Performance: Cache repeated computations in MappedData methods #86

@anthropic-code-agent

Description

@anthropic-code-agent

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

  1. Store intermediate results: Save computed values in local variables
  2. Use R6 private fields: For class methods, cache expensive computations in private fields
  3. Implement memoization: Use function memoization for pure functions with repeated calls
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions