Repository navigation
Reduce method invalidations on load (916 -> 191) - #345
Merged
Merged
Conversation
Loading DiskArrays invalidated ~900 MethodInstances, mostly from methods
that add to Base.Generator(f, ::Any) and zip(x, ::AbstractDiskArray, ...).
- Specialize collect/map on Base.Generator{<:AbstractDiskArray} rather than
overriding the Base.Generator constructor; DiskGenerator is now an alias.
- Remove the untyped catch-all zip error methods.
- Give ChunkIndices the right eltype through its supertype instead of an
eltype method.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
meggart
approved these changes
Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Loading DiskArrays currently invalidates ~900
MethodInstances in a fresh session (measured with SnoopCompile@snoop_invalidationson Julia 1.13,using DiskArrays). Almost all of it comes from three definitions. This PR removes them while keeping behavior.main(v0.4.25)Breakdown on
main:Base.Generator(f, A::AbstractDiskArray)(via@implement_generator): ~712. The first argument is untyped, so every compiledBase.Generator(f, ::Any)call site in Base/stdlib/Pkg is invalidated.Base.zip(::AbstractDiskArray, x, xs...),Base.zip(x, ::AbstractDiskArray, xs...),Base.zip(x::AbstractDiskArray, ::AbstractDiskArray, xs...)(the catch-all_zip_errormethods): ~172 (the typedDiskZipmethods remain, see "Not addressed").Base.eltype(::Type{<:ChunkIndices{N,<:Any,O}}): ~18.Why this matters: downstream packages that depend on DiskArrays (HDF5.jl is considering making
Dataset <: AbstractDiskArray, JuliaIO/HDF5.jl#1246) otherwise pass this cost on to every user session.Changes
DiskGeneratoris nowconst DiskGenerator = Base.Generator{<:AbstractDiskArray}. Chunk-ordered collection is implemented by specializingcollect(::Generator{<:T})andmap(f, ::T)instead of changing what theGeneratorconstructor returns. (map(f, ::AbstractArray)iscollect_similar(A, Generator(f, A))in Base, so the previouscollect_similarmethod is replaced by amapmethod.)[f(x) for x in disk_array]andmap(f, disk_array)read chunk by chunk as before (existing tests,getindex_count == 6).@implement_generator tkeeps working for downstream types and now defines those methods.ArgumentError("Cannot zip a disk array with an iterator"). Zipping a disk array with a non-array iterator now falls back toBase.zip.ChunkIndices{N,RT,O<:ChunkIndexType} <: AbstractArray{ChunkIndex{N,O},N}, soeltypeis right from the supertype and the extraeltypemethod is dropped.Behavior changes to review
zip(disk_array, non_array_iterator)no longer throws; it returns aBase.Iterators.Zip. A method with an untyped argument cannot avoid invalidations, so I dropped the error; happy to discuss alternatives.Base.Generator(f, disk_array)now returns a plainBase.Generator, not a separateDiskGeneratorstruct.g isa DiskGeneratorstill holds.size,length,keysetc. come from Base.ChunkIndicesnow requiresO <: ChunkIndexType.Not addressed
The typed
zip(::AbstractArray, ::AbstractDiskArray, ...)methods that returnDiskZipstill invalidate ~170 instances, which is most of the remaining cost. Avoiding that would needDiskZipto be produced by something other thanzipdispatch (e.g. specializingcollectonIterators.Zip{<:Tuple{<:AbstractDiskArray,...}}), which is a larger design change. Happy to follow up.Testing
mapresult order and chunk read count,zipwith a non-array iterator, and a guard that DiskArrays defines no methods on theBase.Generatorconstructor.