Skip to content

fix: handle broadcast allocation for range-backed QuantityArray#218

Merged
MilesCranmer merged 2 commits into
JuliaPhysics:mainfrom
MilesCranmerBot:fix/issue-217
Apr 23, 2026
Merged

fix: handle broadcast allocation for range-backed QuantityArray#218
MilesCranmer merged 2 commits into
JuliaPhysics:mainfrom
MilesCranmerBot:fix/issue-217

Conversation

@MilesCranmerBot

Copy link
Copy Markdown
Contributor

Fixes #217.

Summary

  • allocate plain arrays when broadcasting over range-backed QuantityArrays into non-range element types
  • preserve the existing fallback for array-like backends such as StaticArrays
  • add a regression test for broadcasting over a StepRangeLen-backed QuantityArray

Testing

  • /usr/local/bin/julia --project=. -e 'using Pkg; Pkg.test()'

Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
@github-actions

github-actions Bot commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1.10)

Time benchmarks
main 1953e8d... main / 1953e8d...
Quantity/creation/Quantity(x) 3.41 ± 0.001 ns 3.41 ± 0.001 ns 1 ± 0.00042
Quantity/creation/Quantity(x, length=y) 3.1 ± 0.01 ns 4.02 ± 0.87 ns 0.771 ± 0.17
Quantity/with_numbers/*real 3.1 ± 0.01 ns 3.1 ± 0.01 ns 1 ± 0.0046
Quantity/with_numbers/^int 8.05 ± 2.2 ns 8.05 ± 2.2 ns 1 ± 0.38
Quantity/with_numbers/^int * real 8.36 ± 2.2 ns 8.36 ± 2.2 ns 1 ± 0.37
Quantity/with_quantity/+y 4.04 ± 0.001 ns 4.04 ± 0.001 ns 1 ± 0.00035
Quantity/with_quantity//y 3.1 ± 0.01 ns 3.1 ± 0.01 ns 1 ± 0.0046
Quantity/with_self/dimension 3.1 ± 0.01 ns 3.1 ± 0.01 ns 1 ± 0.0046
Quantity/with_self/inv 3.11 ± 0.01 ns 3.11 ± 0.01 ns 1 ± 0.0046
Quantity/with_self/ustrip 2.79 ± 0 ns 2.79 ± 0 ns 1 ± 0
QuantityArray/broadcasting/multi_array_of_quantities 0.143 ± 0.0055 ms 0.143 ± 0.0034 ms 1 ± 0.045
QuantityArray/broadcasting/multi_normal_array 0.0516 ± 0.0031 ms 0.0527 ± 0.003 ms 0.979 ± 0.081
QuantityArray/broadcasting/multi_quantity_array 0.155 ± 0.00089 ms 0.155 ± 0.0086 ms 0.998 ± 0.055
QuantityArray/broadcasting/x^2_array_of_quantities 24.6 ± 3 μs 23.5 ± 2.1 μs 1.04 ± 0.16
QuantityArray/broadcasting/x^2_normal_array 5.12 ± 0.84 μs 4.75 ± 0.79 μs 1.08 ± 0.25
QuantityArray/broadcasting/x^2_quantity_array 6.93 ± 0.29 μs 6.94 ± 0.29 μs 0.999 ± 0.059
QuantityArray/broadcasting/x^4_array_of_quantities 0.0815 ± 0.0037 ms 0.0817 ± 0.00073 ms 0.998 ± 0.046
QuantityArray/broadcasting/x^4_normal_array 0.0492 ± 0.0031 ms 0.048 ± 0.0031 ms 1.02 ± 0.092
QuantityArray/broadcasting/x^4_quantity_array 0.0502 ± 0.0031 ms 0.05 ± 0.00022 ms 1 ± 0.062
time_to_load 0.209 ± 0.0016 s 0.207 ± 0.0024 s 1.01 ± 0.014
Memory benchmarks
main 1953e8d... main / 1953e8d...
Quantity/creation/Quantity(x) 0 allocs: 0 B 0 allocs: 0 B
Quantity/creation/Quantity(x, length=y) 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/*real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int * real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity/+y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity//y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/dimension 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/inv 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/ustrip 0 allocs: 0 B 0 allocs: 0 B
QuantityArray/broadcasting/multi_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/multi_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/multi_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^2_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^2_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^2_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^4_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^4_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^4_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
time_to_load 0.153 k allocs: 14.5 kB 0.153 k allocs: 14.5 kB 1

@github-actions

github-actions Bot commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
main 1953e8d... main / 1953e8d...
Quantity/creation/Quantity(x) 2.31 ± 0.002 ns 2.31 ± 0.002 ns 0.999 ± 0.0012
Quantity/creation/Quantity(x, length=y) 2.33 ± 0.11 ns 2.6 ± 0.003 ns 0.893 ± 0.041
Quantity/with_numbers/*real 2.62 ± 0.002 ns 2.62 ± 0.036 ns 0.998 ± 0.014
Quantity/with_numbers/^int 12.6 ± 1.2 ns 12.6 ± 1.2 ns 0.999 ± 0.13
Quantity/with_numbers/^int * real 11.8 ± 1.4 ns 12.3 ± 1.2 ns 0.955 ± 0.15
Quantity/with_quantity/+y 4.04 ± 0.002 ns 3.89 ± 0.003 ns 1.04 ± 0.00095
Quantity/with_quantity//y 2.31 ± 0.002 ns 2.32 ± 0.002 ns 1 ± 0.0012
Quantity/with_self/dimension 2.6 ± 0.002 ns 2.03 ± 0.004 ns 1.28 ± 0.0027
Quantity/with_self/inv 2.31 ± 0.002 ns 2.32 ± 0.29 ns 0.999 ± 0.12
Quantity/with_self/ustrip 2.33 ± 0.014 ns 2.31 ± 0.001 ns 1.01 ± 0.0061
QuantityArray/broadcasting/multi_array_of_quantities 0.121 ± 0.013 ms 0.12 ± 0.013 ms 1.01 ± 0.15
QuantityArray/broadcasting/multi_normal_array 0.0496 ± 0.0032 ms 0.0493 ± 0.0029 ms 1.01 ± 0.088
QuantityArray/broadcasting/multi_quantity_array 0.0521 ± 0.0028 ms 0.0526 ± 0.0029 ms 0.991 ± 0.076
QuantityArray/broadcasting/x^2_array_of_quantities 0.0374 ± 0.02 ms 0.0378 ± 0.011 ms 0.99 ± 0.61
QuantityArray/broadcasting/x^2_normal_array 4 ± 4 μs 4.04 ± 3.8 μs 0.991 ± 1.4
QuantityArray/broadcasting/x^2_quantity_array 4.94 ± 0.47 μs 6.2 ± 0.41 μs 0.796 ± 0.092
QuantityArray/broadcasting/x^4_array_of_quantities 0.0934 ± 0.01 ms 0.0939 ± 0.0081 ms 0.995 ± 0.14
QuantityArray/broadcasting/x^4_normal_array 0.0449 ± 0.002 ms 0.0454 ± 0.0018 ms 0.99 ± 0.059
QuantityArray/broadcasting/x^4_quantity_array 0.0454 ± 0.0024 ms 0.0452 ± 0.0022 ms 1 ± 0.071
time_to_load 0.185 ± 0.002 s 0.183 ± 0.002 s 1.01 ± 0.016
Memory benchmarks
main 1953e8d... main / 1953e8d...
Quantity/creation/Quantity(x) 0 allocs: 0 B 0 allocs: 0 B
Quantity/creation/Quantity(x, length=y) 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/*real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int * real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity/+y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity//y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/dimension 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/inv 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/ustrip 0 allocs: 0 B 0 allocs: 0 B
QuantityArray/broadcasting/multi_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/multi_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/multi_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^2_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^2_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^2_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^4_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^4_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^4_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
time_to_load 0.145 k allocs: 11 kB 0.145 k allocs: 11 kB 1

@codecov

codecov Bot commented Apr 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.16%. Comparing base (0341715) to head (1953e8d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #218   +/-   ##
=======================================
  Coverage   99.16%   99.16%           
=======================================
  Files          23       23           
  Lines        1320     1320           
=======================================
  Hits         1309     1309           
  Misses         11       11           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/arrays.jl
Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
@MilesCranmer MilesCranmer merged commit fcb754c into JuliaPhysics:main Apr 23, 2026
7 checks passed
github-actions Bot referenced this pull request Apr 23, 2026
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

Bug in show using a QuantityArray made from a StepRangeLen

2 participants