Narrow Zero arithmetic methods to reduce invalidations#348
Narrow Zero arithmetic methods to reduce invalidations#348odow merged 1 commit intojump-dev:masterfrom
Conversation
The methods +(x::Any, ::Zero), *(::Zero, ::Any), etc. used untyped arguments, causing method invalidations by superseding the fundamental +(x, y) and *(x, y) fallbacks in Base. Narrow these to Number, AbstractArray, and AbstractMutable since these cover the types that participate in MutableArithmetics rewrites. Downstream packages with custom types can define their own +(::MyType, ::Zero) methods (as MultivariatePolynomials already does).
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #348 +/- ##
==========================================
- Coverage 91.79% 91.40% -0.40%
==========================================
Files 23 23
Lines 2292 2304 +12
==========================================
+ Hits 2104 2106 +2
- Misses 188 198 +10 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
This seems reasonable, but @blegat might need to chime in. I'll look into the allocation issues in a separate PR. |
|
👏 |
|
:) |
| Base.:*(z::Zero) = z | ||
|
|
||
| function Base.:/(z::Zero, x::Any) | ||
| function Base.:/(z::Zero, x::Number) |
There was a problem hiding this comment.
This broke PowerModels: https://github.com/lanl-ansi/PowerModels.jl/actions/runs/23859045899/job/69560323741?pr=994
I'll make a fix
There was a problem hiding this comment.
Maybe we can add integration tests for downstream packages to CI?
There was a problem hiding this comment.
Yes, we have something similar for MathOptInterface
|
The |
Summary
+(x::Any, ::Zero),*(::Zero, ::Any),-(x::Any, ::Zero),and
/(::Zero, x::Any)from::AnytoNumber,AbstractArray, andAbstractMutableAbstractMutablemethods todispatch.jl(where the type is defined)Motivation
Loading MutableArithmetics causes 1,489 method invalidations. After this PR: 1,317 (-172).
The remaining MA-owned invalidations (462) are from
MutatingStepRange <: OrdinalRangewhich is inherent to subtypingOrdinalRangeand not addressable by type narrowing. The rest (855) come from SparseArrays/Test stdlibs.Before (v1.6.7 released)
+(x::Any, ::Zero)step(r::MutatingStepRange)*(::Zero, ::Any)iterate(r::MutatingStepRange, i)After (this PR)
step(r::MutatingStepRange)iterate(r::MutatingStepRange, i)+(x::Number, ::Zero)*(::Number, z::Zero)The Zero
+/*/-//invalidations are fully eliminated by this PR. TheMutatingStepRangeinvalidations remain but are a separate issue.How to reproduce
In a fresh Julia session:
Test plan