Skip to content

Commit aabb06f

Browse files
authored
Avoid allocations in checking == with UniformScaling (#1367)
This improves performance and reduces allocations. E.g., ```julia julia> A = sprandn(90,90, 0.01); julia> @Btime $A == 2I # master 807.674 ns (12 allocations: 4.97 KiB) false julia> @Btime $A == 2I # this PR 18.746 ns (0 allocations: 0 bytes) false ```
1 parent b6ad8f9 commit aabb06f

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/uniformscaling.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ function ==(A::AbstractMatrix, J::UniformScaling)
338338
size(A, 1) == size(A, 2) || return false
339339
iszero(J.λ) && return iszero(A)
340340
isone(J.λ) && return isone(A)
341-
return A == J.λ*one(A)
341+
isdiag(A) || return false
342+
return all(==(J.λ), diagview(A))
342343
end
343344
function ==(A::StridedMatrix, J::UniformScaling)
344345
size(A, 1) == size(A, 2) || return false

0 commit comments

Comments
 (0)