Skip to content

Commit fdc1090

Browse files
jishnubmbauman
andauthored
Specialize newindex for StructuredMatrix broadcasting (#55626)
This provides most of the benefits seen in #55604. The simpler implementation appears to help with branch-prediction in inferring the zero elements of the structured matrices. The improved performance as a consequence: ```julia julia> using LinearAlgebra julia> U = UpperTriangular(rand(3000,3000)); D = Diagonal(rand(size(U,1))); julia> @Btime $U .+ $D; 23.405 ms (3 allocations: 68.66 MiB) # nightly 15.266 ms (3 allocations: 68.66 MiB) # This PR ``` --------- Co-authored-by: Matt Bauman <[email protected]>
1 parent da3468c commit fdc1090

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

stdlib/LinearAlgebra/src/structuredbroadcast.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ function Broadcast.newindex(A::StructuredMatrix, b::BandIndex)
199199
# and we apply newindex to both the axes at once to obtain the result
200200
size(A,1) > 1 ? b : BandIndex(0, 1)
201201
end
202+
# All structured matrices are square, and therefore they only broadcast out if they are size (1, 1)
203+
Broadcast.newindex(D::StructuredMatrix, I::CartesianIndex{2}) = size(D) == (1,1) ? CartesianIndex(1,1) : I
202204

203205
function copyto!(dest::Diagonal, bc::Broadcasted{<:StructuredMatrixStyle})
204206
isvalidstructbc(dest, bc) || return copyto!(dest, convert(Broadcasted{Nothing}, bc))

0 commit comments

Comments
 (0)