Skip to content

[WIP] Optimize unit conversion operations to reduce .NET interop calls#1797

Closed
Claude wants to merge 1 commit intomainfrom
claude/optimize-unit-conversion-operations
Closed

[WIP] Optimize unit conversion operations to reduce .NET interop calls#1797
Claude wants to merge 1 commit intomainfrom
claude/optimize-unit-conversion-operations

Conversation

@Claude
Copy link
Copy Markdown

@Claude Claude AI commented Mar 4, 2026

Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.

Original prompt

This section details on the original issue you should resolve

<issue_title>Optimize unit conversion operations to reduce .NET interop calls</issue_title>
<issue_description>## Issue Overview

Reduce multiple .NET calls per unit conversion operation. Currently up to 4 .NET calls per toUnit() invocation.

Current Problem

File: R/utilities-units.R:148-242

Benchmark evidence shows significant overhead:

  • 70k ms for 5000 iterations with 100k values
  • Memory usage: 14,753 MB indicating heavy allocations
  • Existing comments show "New version: 7k ms, 2290 mb" vs old "40k ms, 10556 mb" - indicating 82% improvement is possible
toUnit <- function(
  quantityOrDimension,
  values,
  targetUnit,
  sourceUnit = NULL,
  molWeight = NULL,
  molWeightUnit = NULL
) {
  # Up to 4 separate .NET calls:
  # 1. Convert molWeight to base unit
  # 2. Convert values to base unit
  # 3. Convert to target unit
  # Plus unnecessary as.numeric(c(values)) creating copies
}

Proposed Solution

  1. Add early returns for same-unit conversions
  2. Eliminate unnecessary as.numeric(c(values)) copies
  3. Check if source and target are the same BEFORE any .NET calls
  4. Combine multiple .NET calls where possible
toUnit <- function(
  quantityOrDimension,
  values,
  targetUnit,
  sourceUnit = NULL,
  molWeight = NULL,
  molWeightUnit = NULL
) {
  # Early return for same unit without source
  if (is.null(sourceUnit) && targetUnit == baseUnit) {
    return(values)
  }

  # Avoid unnecessary memory allocation if values are already numeric
  if (!is.numeric(values)) {
    values <- as.numeric(values)
  }

  # Check if source and target are the same BEFORE any .NET calls
  if (!is.null(sourceUnit) && sourceUnit == targetUnit) {
    return(values)
  }

  # Minimize .NET calls...
}

Expected Impact

  • Priority: CRITICAL
  • Estimated Impact: 50-80% reduction in unit conversion time (evidence from existing benchmark)
  • Effort: Medium-High
  • Testing: Run existing benchmark script tests/dev/script-benchmark-unitConversion.R to verify improvements

Evidence

From tests/dev/script-benchmark-unitConversion.R:

  • Same unit conversion: 40+ ms per 1000 iterations
  • Comments show 82% improvement is achievable with optimization

Parent Issue

This is part of the comprehensive performance optimization analysis in #1765


Parent Issue: #1765</issue_description>

Comments on the Issue (you are @claude[agent] in this section)

@Claude Claude AI assigned Claude and PavelBal Mar 4, 2026
Copilot stopped work on behalf of PavelBal due to an error March 4, 2026 16:42
@PavelBal PavelBal closed this Mar 5, 2026
@PavelBal PavelBal deleted the claude/optimize-unit-conversion-operations branch March 5, 2026 13:49
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.

Optimize unit conversion operations to reduce .NET interop calls

2 participants