Repository navigation
Convert once per distinct pair of units instead of once per value - #445
Merged
Enchufa2 merged 4 commits intoSep 28, 2026
Merged
Conversation
ud_convert() and ud_are_convertible() called into udunits once per element through mapply(), so converting a units vector of n values made n native calls, each parsing both units and building a converter. That includes every units<- conversion, which passes a single pair of units. Convert the whole vector in one call when there is a single pair of units, and otherwise once per distinct pair, writing the results back in order. Arguments whose lengths mapply() would warn about or refuse, and non-atomic x, still go through mapply(), so results, names, warnings and errors are unchanged. Tests compare against element by element conversion over a grid of inputs and 50 random mixes of units, and pin order, names, NA, zero lengths and recycling. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mixed_units() called set_units() once per element, parsing the unit string for every value, and set_units() on mixed units rebuilt its result the same way after grouping its values with keys from as.character(), which pretty-prints every element's units. Build one units vector per distinct unit and split it into one units object per value, and group with ud_char() keys instead. Results are identical to building the elements one by one, which the tests check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #445 +/- ##
==========================================
+ Coverage 92.56% 92.92% +0.36%
==========================================
Files 20 20
Lines 1156 1201 +45
==========================================
+ Hits 1070 1116 +46
+ Misses 86 85 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ud_convert()andud_are_convertible()called into udunits once per element throughmapply(). Converting a units vector of n values therefore made n native calls, and each one parsed both units and built a converter. That includes everyunits<-conversion, which passes a single pair of units. Mixed units had the same per-element cost:mixed_units()calledset_units()once per element, parsing the unit string for every value.set_units()on mixed units grouped its values with keys fromas.character(), which pretty-prints every element's units.Changes
ud_convert()andud_are_convertible()convert the whole vector in one call when there is a single pair of units. Otherwise they convert once per distinct pair and write the results back in order.mapply()would warn about or refuse, and non-atomicx, still go throughmapply(). Results, names, warnings and errors are therefore unchanged.mixed_units()builds one units vector per distinct unit and splits it into one units object per value.set_units()on mixed units groups withud_char()keys.Behaviour is unchanged
All results are
identical()to element-by-element conversion.NA/NaN/Inf, zero-length input, the recycling warning, listx, error cases, and mixed-units construction and conversion.Speed
At 10^6 values:
set_units(), single unit pairx + yud_convert(), mixed unitsmixed_units()set_units()on mixed unitsEvery size from 100 values up is faster.
Relationship to other open PRs
Trial merges with #443 and with the long-unit-format fix conflict only in
NEWS.mdandtests/testthat/test_udunits.R, where each branch appends entries. Keep both sides.R/udunits.Rmerges cleanly with #443.🤖 Generated with Claude Code