Skip to content

Performance: Vectorize loops in MappedDataTimeProfile.R #84

@anthropic-code-agent

Description

@anthropic-code-agent

Description

Optimize loops in R/MappedDataTimeProfile.R by replacing them with vectorized operations for improved performance.

Current Implementation Issues

Loop in scaleDataForSecondaryAxis() (Lines 252-263)

  • Iterates through scaling-relevant mappings one by one
  • Can be replaced with vectorized operations

Loop in dataForPlot() (Lines 323-345)

  • Iterates through aesthetics checking for negative values
  • Inefficient for datasets with many aesthetics
  • Repeatedly accesses data frame columns in loop

Loop in checkForCallAesthetics() (Lines 365-389)

  • Iterates through scaling-relevant mappings sequentially
  • Similar pattern to scaleDataForSecondaryAxis()

Nested loop in setyLimits() (Lines 410-429)

  • Iterates through aesthetics within axis loop
  • Double iteration pattern that can be optimized

Suggested Implementation

  1. scaleDataForSecondaryAxis(): Use lapply() or vapply() with vectorized scaling operations
  2. dataForPlot(): Use vectorized column checking with any() and all() functions across aesthetics
  3. checkForCallAesthetics(): Refactor to use list operations instead of explicit loops
  4. setyLimits(): Flatten nested structure or use expand.grid() approach

Expected Benefits

  • Faster time profile plot generation
  • Better performance with large time-series datasets
  • Reduced memory allocations during plotting

Implementation Notes

Files to Modify

  • R/MappedDataTimeProfile.R

Testing

  • Run existing time profile tests
  • Test with various secondary axis configurations
  • Verify scaling calculations remain accurate
  • Test with large time-series datasets

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