Skip to content

Consult udunits once per distinct symbol in as_units() - #441

Merged
Enchufa2 merged 2 commits into
r-quantities:mainfrom
billdenney:perf-dedupe-udunits-lookups
Sep 22, 2026
Merged

Enchufa2 merged 2 commits into
r-quantities:mainfrom
billdenney:perf-dedupe-udunits-lookups

Conversation

@billdenney

Copy link
Copy Markdown
Contributor

Follow-up to #439, split out as requested there.

Fixes #440

The tokenizer represents a power as one copy of the symbol per unit of exponent, and as_units.character() then called ud_is_parseable() and ud_get_symbol() once per copy, so m255 s-255 cost 510 udunits round trips for two distinct symbols. This looks each distinct symbol up once and maps the results back with match(). As a side effect an unrecognized symbol is reported once ('x' is not recognized) instead of once per copy, which the new test pins.

Median of 20 runs of as_units() with bench::mark, before and after:

case string before after
typical kg m-2 s-1 0.26 ms 0.25 ms
moderate kg2 m4 s-6 A-2 0.54 ms 0.29 ms
max exponent m255 s-255 14.15 ms 0.43 ms
unknown symbol x-2000 1160.25 ms 2.14 ms

🤖 Generated with Claude Code

billdenney and others added 2 commits September 22, 2026 18:59
The tokenizer represents a power as one copy of the symbol per unit of
exponent, and as_units.character() then called ud_is_parseable() and
ud_get_symbol() once per copy, so "m255 s-255" cost 510 udunits round
trips for two distinct symbols. Look each distinct symbol up once and
map the results back with match(). As a side effect an unrecognized
symbol is now reported once ("'x' is not recognized") instead of once
per copy.

Median of 20 runs of as_units() with bench::mark, before and after:

    case          string            before      after
    typical       kg m-2 s-1         0.26 ms    0.25 ms
    moderate      kg2 m4 s-6 A-2     0.54 ms    0.29 ms
    max_exponent  m255 s-255        14.15 ms    0.43 ms
    unknown       x-2000          1160.25 ms    2.14 ms

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@Enchufa2
Enchufa2 merged commit 14cdd66 into r-quantities:main Sep 22, 2026
8 checks passed
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.

Consult udunits once per distinct symbol in as_units()

2 participants