Skip to content

Reduce method invalidations on load (916 -> 191) - #345

Merged
meggart merged 1 commit into
JuliaIO:mainfrom
mkitti:reduce-invalidations
Nov 24, 2026
Merged

meggart merged 1 commit into
JuliaIO:mainfrom
mkitti:reduce-invalidations

Conversation

@mkitti

@mkitti mkitti commented Oct 8, 2026

Copy link
Copy Markdown
Member

Summary

Loading DiskArrays currently invalidates ~900 MethodInstances in a fresh session (measured with SnoopCompile @snoop_invalidations on Julia 1.13, using DiskArrays). Almost all of it comes from three definitions. This PR removes them while keeping behavior.

invalidated MethodInstances
main (v0.4.25) 916
this PR 191

Breakdown on main:

  • Base.Generator(f, A::AbstractDiskArray) (via @implement_generator): ~712. The first argument is untyped, so every compiled Base.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_error methods): ~172 (the typed DiskZip methods 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

  • Generators. DiskGenerator is now const DiskGenerator = Base.Generator{<:AbstractDiskArray}. Chunk-ordered collection is implemented by specializing collect(::Generator{<:T}) and map(f, ::T) instead of changing what the Generator constructor returns. (map(f, ::AbstractArray) is collect_similar(A, Generator(f, A)) in Base, so the previous collect_similar method is replaced by a map method.) [f(x) for x in disk_array] and map(f, disk_array) read chunk by chunk as before (existing tests, getindex_count == 6). @implement_generator t keeps working for downstream types and now defines those methods.
  • zip. Removed the catch-all methods that threw ArgumentError("Cannot zip a disk array with an iterator"). Zipping a disk array with a non-array iterator now falls back to Base.zip.
  • ChunkIndices. ChunkIndices{N,RT,O<:ChunkIndexType} <: AbstractArray{ChunkIndex{N,O},N}, so eltype is right from the supertype and the extra eltype method is dropped.

Behavior changes to review

  1. zip(disk_array, non_array_iterator) no longer throws; it returns a Base.Iterators.Zip. A method with an untyped argument cannot avoid invalidations, so I dropped the error; happy to discuss alternatives.
  2. Base.Generator(f, disk_array) now returns a plain Base.Generator, not a separate DiskGenerator struct. g isa DiskGenerator still holds. size, length, keys etc. come from Base.
  3. ChunkIndices now requires O <: ChunkIndexType.

Not addressed

The typed zip(::AbstractArray, ::AbstractDiskArray, ...) methods that return DiskZip still invalidate ~170 instances, which is most of the remaining cost. Avoiding that would need DiskZip to be produced by something other than zip dispatch (e.g. specializing collect on Iterators.Zip{<:Tuple{<:AbstractDiskArray,...}}), which is a larger design change. Happy to follow up.

Testing

  • Full test suite passes locally.
  • Added tests: map result order and chunk read count, zip with a non-array iterator, and a guard that DiskArrays defines no methods on the Base.Generator constructor.
  • To measure:
    using SnoopCompileCore
    invs = @snoop_invalidations using DiskArrays
    using SnoopCompile
    trees = invalidation_trees(invs)
    sum(SnoopCompile.countchildren, trees)

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
meggart merged commit 563a7e9 into JuliaIO:main Nov 24, 2026
12 checks passed
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.

2 participants