Skip to content

Commit 0a253be

Browse files
authored
Explicit loop in _iszero for strided views (#1264)
This reduces TTFX ```julia julia> using LinearAlgebra julia> Z = zeros(4,4); julia> @time istril(Z); 0.152632 seconds (572.99 k allocations: 27.476 MiB, 99.98% compilation time) # master 0.091427 seconds (177.39 k allocations: 8.688 MiB, 99.97% compilation time) # this PR ```
1 parent 3f46f5f commit 0a253be

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/generic.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,8 +1358,15 @@ ishermitian(x::Number) = (x == conj(x))
13581358
_iszero(V) = iszero(V)
13591359
# A Base.FastContiguousSubArray view of a StridedArray
13601360
FastContiguousSubArrayStrided{T,N,P<:StridedArray,I<:Tuple{AbstractUnitRange, Vararg{Any}}} = Base.SubArray{T,N,P,I,true}
1361-
# using mapreduce instead of all permits vectorization
1362-
_iszero(V::FastContiguousSubArrayStrided) = mapreduce(iszero, &, V, init=true)
1361+
# Reducing over the entire array instead of calling `all` within `iszero` permits vectorization
1362+
# The loop is equivalent to a mapreduce, but is faster to compile
1363+
function _iszero(V::FastContiguousSubArrayStrided)
1364+
ret = true
1365+
for i in eachindex(V)
1366+
ret &= iszero(@inbounds V[i])
1367+
end
1368+
ret
1369+
end
13631370

13641371
"""
13651372
istriu(A::AbstractMatrix, k::Integer = 0) -> Bool

0 commit comments

Comments
 (0)