Conversation
…r indexing notation
There was a problem hiding this comment.
Pull request overview
This PR adds a unified, lazy slicing/view mechanism for AbstractSparseArray and introduces broadcast support that materializes results as SparseArray, aiming to improve ergonomics and consistency when indexing/slicing sparse containers (including JuMP-backed IndexedVarArray).
Changes:
- Introduces
SparseArraySliceand aslice(sa, mask...)API, and updatesgetindexselection logic to return lazy slices for non-exact selectors. - Adds broadcasting support for
AbstractSparseArrayvia a customBroadcastStyle, producingSparseArrayresults. - Extends
IndexedVarArrayselection with a cache cutoff knob (set_cache_cutoff!) and integrates slice key-projection helpers into cached selection.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| test/testdata.jl | Adds a small SparseArray fixture used by new slice/broadcast tests. |
| test/runtests.jl | Adds coverage for SparseArraySlice behavior and broadcasting across SparseArray, slices, and IndexedVarArray. |
| src/SparseVariables.jl | Wires in new slice.jl/broadcast.jl and exports new APIs/types. |
| src/sparsearray.jl | Introduces _keytype interface and a generic haskey for AbstractSparseArray. |
| src/slice.jl | Implements SparseArraySlice, key projection/reconstruction, and core slice behaviors. |
| src/indexedarray.jl | Adds _keytype for IndexedVarArray, cache cutoff configuration, and slice-specific optimizations (cached key selection + efficient JuMP sum). |
| src/dictionaries.jl | Refactors index classification (isfixed) and routes non-lookup indexing through _make_slice. |
| src/broadcast.jl | Implements broadcast style, key compatibility checks, and materialization into SparseArray. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| isfixed(::Type{T} where {T<:Function}) = false | ||
| isfixed(::Type{T} where {T<:UnitRange}) = false | ||
| iscolon(t) = false | ||
| iscolon(::Type{T} where {T<:Colon}) = true | ||
| isfixed(::Type{T} where {T<:AbstractRange}) = false |
There was a problem hiding this comment.
Fixed in commit Generalize make_filter_fun to handle AbstractRange. Replaced the UnitRange-only 1-arg overload with make_filter_fun(c::AbstractRange) = x -> x in c, and added the missing 2-arg variant make_filter_fun(c::AbstractRange, pos) = x -> x[pos] in c. This keeps selector semantics consistent with isfixed for all AbstractRange subtypes (e.g. StepRange, LinRange).
| return haskey(_data(v.parent), _reconstruct_key(v.mask, free_key, T)) | ||
| end | ||
|
|
||
| Base.length(v::SparseArraySlice) = length(_view_matching_keys(v)) |
There was a problem hiding this comment.
Added memoization in commit Add memoization for _view_matching_keys in SparseArraySlice. A _cache::Ref{Any} field (initialised to nothing via an inner constructor) was added to SparseArraySlice. _view_matching_keys now stores the computed vector in that Ref on the first call and returns it directly on subsequent calls, avoiding repeated linear scans and allocations for the same slice instance.
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
hellemo
left a comment
There was a problem hiding this comment.
I didn't go through everything in detail, but looks good to me overall. A nice upgrade in functionality.
My apologies for a too large PR, but I got a bit carried away :)
The purpose of this PR is two-fold 1) Broadcasting support 2) A more unified approach when slicing an AbstractSparseArray.
There is still some open issues to discuss, both on naming and preferred ergonomics. I have supported old syntax, but have also added a new
slice()function that provides the same syntax as doing it directly when indexing.I will follow up this PR with an updates to benchmarks and documentation (depending upon where it goes).