DynamicQuantities.jl icon indicating copy to clipboard operation
DynamicQuantities.jl copied to clipboard

fix: preserve symbolic external units across precompile

Open MilesCranmerBot opened this issue 7 months ago • 20 comments

Summary

Preserve external symbolic unit registrations across precompile and import boundaries.

Changes

  • thread the caller module through us"..." symbolic macro expansion
  • lazily re-register external bindings when a symbolic unit exists in the caller module but the symbolic registry has not been rebuilt yet
  • add a regression test covering the precompile/import case for external units

Testing

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

Fixes #212

MilesCranmerBot avatar Apr 22 '26 15:04 MilesCranmerBot

Benchmark Results (Julia v1.10)

Time benchmarks
main 7ec2b6cf7212b4... main / 7ec2b6cf7212b4...
Quantity/creation/Quantity(x) 3.41 ± 0.001 ns 3.1 ± 0.01 ns 1.1 ± 0.0036
Quantity/creation/Quantity(x, length=y) 3.1 ± 0.01 ns 3.1 ± 0.01 ns 0.997 ± 0.0045
Quantity/with_numbers/*real 3.1 ± 0.01 ns 3.41 ± 0.01 ns 0.909 ± 0.004
Quantity/with_numbers/^int 8.05 ± 2.2 ns 8.67 ± 2.5 ns 0.929 ± 0.36
Quantity/with_numbers/^int * real 8.36 ± 2.2 ns 8.05 ± 1.9 ns 1.04 ± 0.36
Quantity/with_quantity/+y 4.04 ± 0.001 ns 4.04 ± 0.001 ns 1 ± 0.00035
Quantity/with_quantity//y 3.11 ± 0.01 ns 3.41 ± 0.01 ns 0.912 ± 0.004
Quantity/with_self/dimension 3.1 ± 0.01 ns 4.33 ± 0.01 ns 0.715 ± 0.0028
Quantity/with_self/inv 3.11 ± 0.001 ns 3.1 ± 0.01 ns 1 ± 0.0032
Quantity/with_self/ustrip 3.71 ± 0.92 ns 3.1 ± 0 ns 1.2 ± 0.3
QuantityArray/broadcasting/multi_array_of_quantities 0.144 ± 0.0012 ms 0.147 ± 0.0087 ms 0.978 ± 0.058
QuantityArray/broadcasting/multi_normal_array 0.0529 ± 0.0029 ms 0.055 ± 0.0031 ms 0.961 ± 0.075
QuantityArray/broadcasting/multi_quantity_array 0.156 ± 0.0023 ms 0.157 ± 0.00094 ms 0.991 ± 0.016
QuantityArray/broadcasting/x^2_array_of_quantities 26.8 ± 3.2 μs 27.9 ± 3.2 μs 0.962 ± 0.16
QuantityArray/broadcasting/x^2_normal_array 5.81 ± 0.75 μs 6.33 ± 0.62 μs 0.918 ± 0.15
QuantityArray/broadcasting/x^2_quantity_array 7.12 ± 0.42 μs 7.11 ± 0.39 μs 1 ± 0.081
QuantityArray/broadcasting/x^4_array_of_quantities 0.082 ± 0.0015 ms 0.085 ± 0.00071 ms 0.965 ± 0.019
QuantityArray/broadcasting/x^4_normal_array 0.0499 ± 0.0023 ms 0.0499 ± 0.00022 ms 0.999 ± 0.046
QuantityArray/broadcasting/x^4_quantity_array 0.0532 ± 0.00028 ms 0.0501 ± 0.00027 ms 1.06 ± 0.008
time_to_load 0.219 ± 0.01 s 0.221 ± 0.014 s 0.989 ± 0.076
Memory benchmarks
main 7ec2b6cf7212b4... main / 7ec2b6cf7212b4...
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[bot] avatar Apr 22 '26 16:04 github-actions[bot]

Benchmark Results (Julia v1)

Time benchmarks
main 7ec2b6cf7212b4... main / 7ec2b6cf7212b4...
Quantity/creation/Quantity(x) 2.32 ± 0.004 ns 2.03 ± 0.002 ns 1.14 ± 0.0023
Quantity/creation/Quantity(x, length=y) 2.6 ± 0.007 ns 2.6 ± 0.49 ns 1 ± 0.19
Quantity/with_numbers/*real 2.32 ± 0.37 ns 2.6 ± 0.14 ns 0.892 ± 0.15
Quantity/with_numbers/^int 12.4 ± 1.2 ns 12.6 ± 1.2 ns 0.978 ± 0.13
Quantity/with_numbers/^int * real 12.1 ± 1.2 ns 11.8 ± 1.2 ns 1.02 ± 0.15
Quantity/with_quantity/+y 3.89 ± 0.002 ns 4.04 ± 0.002 ns 0.965 ± 0.00069
Quantity/with_quantity//y 2.6 ± 0.003 ns 2.31 ± 0.003 ns 1.12 ± 0.002
Quantity/with_self/dimension 2.03 ± 0.02 ns 2.31 ± 0.001 ns 0.877 ± 0.0087
Quantity/with_self/inv 2.33 ± 0.28 ns 2.32 ± 0.003 ns 1.01 ± 0.12
Quantity/with_self/ustrip 2.04 ± 0.002 ns 2.04 ± 0.001 ns 1 ± 0.0011
QuantityArray/broadcasting/multi_array_of_quantities 0.126 ± 0.016 ms 0.126 ± 0.018 ms 1 ± 0.19
QuantityArray/broadcasting/multi_normal_array 0.0479 ± 0.0033 ms 0.0479 ± 0.0034 ms 1 ± 0.099
QuantityArray/broadcasting/multi_quantity_array 0.0532 ± 0.0056 ms 0.0517 ± 0.004 ms 1.03 ± 0.13
QuantityArray/broadcasting/x^2_array_of_quantities 0.0448 ± 0.031 ms 0.0432 ± 0.031 ms 1.04 ± 1
QuantityArray/broadcasting/x^2_normal_array 3.53 ± 5.7 μs 3.46 ± 5.3 μs 1.02 ± 2.3
QuantityArray/broadcasting/x^2_quantity_array 4.9 ± 3.6 μs 4.88 ± 0.22 μs 1 ± 0.75
QuantityArray/broadcasting/x^4_array_of_quantities 0.0994 ± 0.027 ms 0.0984 ± 0.026 ms 1.01 ± 0.39
QuantityArray/broadcasting/x^4_normal_array 0.0445 ± 0.0024 ms 0.0446 ± 0.0026 ms 0.999 ± 0.079
QuantityArray/broadcasting/x^4_quantity_array 0.0448 ± 0.0029 ms 0.0448 ± 0.0027 ms 1 ± 0.089
time_to_load 0.191 ± 0.0014 s 0.189 ± 0.0012 s 1.01 ± 0.01
Memory benchmarks
main 7ec2b6cf7212b4... main / 7ec2b6cf7212b4...
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

github-actions[bot] avatar Apr 22 '26 16:04 github-actions[bot]

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 99.18%. Comparing base (c2098b5) to head (9f886d9). :warning: Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #215      +/-   ##
==========================================
+ Coverage   99.15%   99.18%   +0.02%     
==========================================
  Files          23       23              
  Lines        1305     1347      +42     
==========================================
+ Hits         1294     1336      +42     
  Misses         11       11              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

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

codecov[bot] avatar Apr 22 '26 16:04 codecov[bot]

@MilesCranmerBot we need to get to 100% coverage of the diff please

MilesCranmer avatar Apr 22 '26 17:04 MilesCranmer

@MilesCranmerBot code coverage is not yet 100% for the diff. And also see comment about making breaking changes please.

MilesCranmer avatar Apr 22 '26 18:04 MilesCranmer

@MilesCranmerBot needs to add a test where we have a fake package that registers units, and that has a function which uses those units. See the recent issue for the workflow I'd like to enable.

MilesCranmer avatar Apr 23 '26 08:04 MilesCranmer

Done. I pushed this to the PR branch in commit 7645686.

What changed:

  • replaced the bare module test with a fake package layout under test/precompile_test/ExternalUnitRegistration/src/ExternalUnitRegistration.jl
  • kept the registered external unit plus a function that uses us"MyWb" from inside that package
  • updated the regression to check the imported package entrypoints after precompilation/import

Validation:

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

MilesCranmerBot avatar Apr 23 '26 08:04 MilesCranmerBot

@codex review

MilesCranmer avatar Apr 23 '26 09:04 MilesCranmer

@MilesCranmerBot Please address the above feedback

MilesCranmer avatar Apr 23 '26 09:04 MilesCranmer

Addressed the review feedback in 524cb15.

What changed:

  • restored the Constants. guard so module-local quantity bindings no longer shadow symbolic constants like c
  • made lazy external-unit registration use the existing unit-update lock, avoiding duplicate first-use registration races
  • added regression tests for both cases

Validation:

  • /usr/local/bin/julia --project=. -e 'using Pkg; Pkg.test(test_args=["unittests"])'

MilesCranmerBot avatar Apr 23 '26 09:04 MilesCranmerBot

@codex review

MilesCranmer avatar Apr 23 '26 15:04 MilesCranmer

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Hm, I still think this feels too hacky... @MilesCranmerBot could you please change the PR in the following way? Here's a detailed description:

This can be fixed in a cleaner and more symmetric way.

The current PR appears to solve the immediate us"..." failure across precompile/import boundaries, but it does so by making only the symbolic parser caller-module-aware. That leaves u"..." and us"..." on different resolution paths:

  • u_str still expands through map_to_scope(Meta.parse(s))
  • us_str is changed to expand through map_to_scope(__module__, Meta.parse(s))

That asymmetry is hard to justify at the parser API level. External units should have one consistent resolution story, regardless of whether the target is ordinary or symbolic.

A cleaner fix would be:

  1. Make both macros caller-module-aware.

    • Change @u_str in src/uparse.jl to call map_to_scope(__module__, Meta.parse(s))
    • Keep the same for @us_str
  2. Add matching module-aware map_to_scope(mod::Module, ...) overloads on both the ordinary and symbolic sides.

    • map_to_scope(ex::Expr) = map_to_scope(@__MODULE__, ex)
    • map_to_scope(sym::Symbol) = map_to_scope(@__MODULE__, sym)
    • map_to_scope(mod::Module, ex::Expr) for recursive descent
    • map_to_scope(mod::Module, sym::Symbol) for symbol resolution
  3. Share the external-binding detection and lazy registration logic instead of implementing it only for symbolic parsing.

    • The external binding check should live in shared internal code
    • The lazy registration path should also be shared
    • Both u"..." and us"..." should be able to recover an external unit from the caller module when the binding exists but the registration state has not yet been rebuilt in the current import/precompile lifecycle
  4. Keep symbolic lookup independent of SYMBOLIC_UNIT_VALUES[i] cache alignment for correctness.

    • Reconstructing the symbolic quantity from the symbol is a reasonable approach
    • That part of the current PR looks directionally good
    • However, it should be part of a symmetric design rather than paired with a parser-only special case for us_str
  5. Add regression tests for both ordinary and symbolic macros through an external module boundary.

    • u"MyExternalUnit"
    • us"MyExternalUnit"
    • direct use
    • imported helper function use
    • precompile/import style loading
    • threaded first-use if concurrency is relevant

The key point is that the problem does not appear to be that symbolic registration should stop being updated. The issue is that symbolic parsing currently has a more fragile lifecycle around external registrations, especially across precompile/import boundaries. The fix should therefore make the registration and recovery story more robust, but without introducing a macro-level asymmetry between u_str and us_str.

In short, the suggested direction is:

  • make u_str and us_str symmetric
  • share external-unit recovery logic
  • keep symbolic lookup robust to cache rebuild timing
  • test both macros the same way

That should produce a clearer and more maintainable design than fixing only us_str in isolation.

MilesCranmer avatar Apr 23 '26 17:04 MilesCranmer

@MilesCranmerBot CI is failing, please fix.

MilesCranmer avatar Apr 23 '26 17:04 MilesCranmer

@MilesCranmerBot code coverage of the diff is under 100%. Are there methods which aren't possible to hit in a realistic scenario? If so then please remove them. If it is possible to hit them, then ensure 100% diff coverage with testing.

MilesCranmer avatar Apr 23 '26 18:04 MilesCranmer

Addressed this in a8912ae.

The uncovered diff lines were fallback branches in map_to_scope for u"..." / us"..." that looked reachable on paper, but they are not hit in the realistic external-unit flow this PR is fixing. I verified that trying to exercise them without a module binding still errors instead of going through that path, so I removed those dead branches rather than adding a contrived test.

I reran /usr/local/bin/julia --project=. -e 'import Pkg; Pkg.test(coverage=true)', and the full suite passed locally with the current diff lines covered.

MilesCranmerBot avatar Apr 23 '26 19:04 MilesCranmerBot

Addressed in 6efcab1.

What changed:

  • moved the external-unit registration helpers out of uparse.jl into shared internal code, so the parse paths stop reaching back into the parent module with reflective getfield(...) lookups
  • cleaned up the u"..." and us"..." symbol-resolution branches so the error conditions live together and the success path is the shared return block
  • kept builtin unit precedence over similarly named constants like h, while still preserving the external-unit recovery path across precompile/import boundaries

Validation:

  • /usr/local/bin/julia --project=. -e 'using Pkg; Pkg.test(test_args=["unittests"])'\n- /usr/local/bin/julia --project=. -e 'using Pkg; Pkg.test()'\n- /usr/local/bin/julia --project=. -e 'using Pkg; Pkg.test(coverage=true)'

MilesCranmerBot avatar Apr 23 '26 23:04 MilesCranmerBot

@codex review

MilesCranmer avatar Apr 24 '26 21:04 MilesCranmer

Addressed the open review comments in 9f886d9.

What changed:

  • removed the extra external_unit_lookup.jl helper file
  • restored uparse / sym_uparse support for registered units while keeping the external-unit precompile fix
  • simplified the parser branches and removed the GlobalRef(mod, sym) lookup pattern from the external-unit path
  • added explicit regression tests for registered-unit parsing so the changed lines are exercised directly

Local verification:

  • /usr/local/bin/julia --project=. -e 'using Pkg; Pkg.test()' ✅
  • focused external-unit smoke test for uparse / sym_uparse and precompile-style module lookup ✅

I also resolved the review threads that this commit addresses. CI is running on the updated branch now, including the codecov path.

MilesCranmerBot avatar Apr 24 '26 21:04 MilesCranmerBot

@codex review

MilesCranmer avatar Apr 25 '26 03:04 MilesCranmer