Remove redundant any/all methods to reduce invalidations#540
Remove redundant any/all methods to reduce invalidations#540ChrisRackauckas-Claude wants to merge 1 commit intoSciML:masterfrom
Conversation
…tions The custom `any(f::Function, A::ArrayPartition)` and `all(f::Function, ...)` methods caused 585 method invalidations when loading OrdinaryDiffEq, including invalidating critical Compiler internals (531 children from `any(::Compiler.compileable_specialization_check, ::AbstractArray)`). The `f::Function` specializations were redundant since the generic `f` versions already existed, but they conflicted with Base's `any(f::Function, a::AbstractArray; dims)`. The generic `f` versions also caused invalidations for the same reason — any new method on `any` for an `AbstractArray` subtype will invalidate cached `any(f, ::AbstractArray)` specializations. Since `ArrayPartition` implements proper iteration (via `eachindex` and `getindex`), the default `AbstractArray` implementations of `any` and `all` work correctly. Removing the custom methods eliminates the invalidation source with no behavioral change. Verified: `any(x->x>4, A)`, `all(x->x>0, A)`, `any(iszero, A)`, `any(A)` etc. all produce correct results via the default AbstractArray path. Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
|
The defaults do work correctly but much slower: Many |
|
Though passing |
Deep analysis of the invalidation mechanismRoot causeIn the Julia 1.12 sysimage, there are exactly 2 MethodInstances (MIs) for
When RecursiveArrayTools loads and defines Why removing only the
|
|
This really comes from a choice in Base JuliaLang/julia#61184 so I'll take it there. |
Summary
Remove custom
anyandallmethod specializations onArrayPartitionthat cause 585 method invalidations when loading packages in the OrdinaryDiffEq dependency chain.Problem
The methods:
The
f::Functionspecializations conflict with Base'sany(f::Function, a::AbstractArray; dims), causing invalidation of cached method specializations. This invalidates 585 downstream methods including:any(::typeof(isnothing), ::AbstractArray)(54 children)any(::Compiler.compileable_specialization_check, ::AbstractArray)(531 children)Additionally, the
all(f, A::ArrayPartition) = all(f, (all(f, x) for x in A.x))had a subtle bug — passingfto the outerallis incorrect since the generator already yields booleans.Fix
Remove all custom
any/allmethods. SinceArrayPartition <: AbstractArrayand implements proper iteration, the defaultAbstractArrayimplementations work correctly.Tested manually:
The pre-existing Zygote adjoint test failure in the test suite is unrelated to this change (occurs on master as well).
Test plan
any/allproduce correct results via default AbstractArray iterationfilterstill works (unchanged)🤖 Generated with Claude Code