SciML / SciML/ComponentArrays.jl
getindex(::ComponentVector, ::Symbol) returns a copy, while getproperty returns a view
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 375
- Forks
- 42
- Avg merge
- 7h 25m
- Merged PRs (30d)
- 17
Description
Updating fields with vectors contained inside them returns a copy of the vector rather than a view into the vector. In addition to preventing the original flat vector from being modified, this allocates a new array.
julia> u = ComponentArray(a = [0.0, 0.0], b = [0.0, 0.0])
julia> getindex(u, :a)
2-element Vector{Float64}: #notice it's a plain vector and not a view
0.0
0.0
julia> getproperty(u, :a)
2-element view(::Vector{Float64}, 1:2) with eltype Float64:
0.0
0.0
julia> u.a.= 0.0
julia> u[:a][1] += 1.0
julia> u.a[1]
0.0 (should be 1.0)
julia> u.a.= 0.0
julia> u.a[1] += 1.0
julia> u.a[1]
1.0 (works fine)
Why do we need this?
This is related to issue: #338
I'm going to post an additional issue about this, but if we ever had something like this:
julia> u = ComponentArray(mass_fractions = (methanol = zeros(n_cells), water = zeros(n_cells)))
and we wanted to loop through it, doing something like:
for cell_id in 1:n_cells
for species_name in keys(u.mass_fractions)
u.mass_fractions[species_name][cell_id] += 1.0
end
end
the original array would not be modified.
The for species_name in keys(u.mass_fractions) is a terrible pattern for performance which you can see here:
https://gist.github.com/Jolt8/3b7cf79326f9ffbee6626a0d39e6e428
Julia Version 1.12.0
ComponentArrays v0.15.32
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Julia examples with ComponentArrays v0.15.32, comparing getindex(u, :a) with getproperty(u, :a). Then locate the getindex and getproperty implementations and add or update tests showing that symbol indexing returns a view whose mutations affect the original flat vector.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100