Commit 0570202
SparseArrays: specialize zero(...) so result has nnz(...) = 0 (#34209)
Before this commit, the result of
zero(x::AbstractSparseArray)
is filled with stored zeros exactly where `x` has a stored value. That is a
wasteful artifact of the default fallback implementation for `AbstractArray`.
After this commit, we return a sparse array of the same dimensions as `x`
without any stored values.
This change is backwards incompatible where it relates to object identity.
Before this commit, for mutable objects, every stored zero has the same
identity. Example:
julia> using SparseArrays
julia> y = zero(BigInt[1,2,3]); y[1] === y[2]
true
julia> y = zero(sparse(BigInt[1,2,3])); y[1] === y[2]
true
julia> Base.zero(a::AbstractSparseArray) = spzeros(eltype(a), size(a)...)
julia> y = zero(sparse(BigInt[1,2,3])); y[1] === y[2]
false
But this behaviour should be classified as a performance bug and can therefore
be fixed in a minor (but not a patch) release.1 parent acb7bd9 commit 0570202
3 files changed
+8
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
102 | 106 | | |
103 | 107 | | |
104 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
0 commit comments