Skip to content

Commit 9c3a15b

Browse files
committed
_sparse_findnextnz: search m.colptr using searchsorted for performance
Thanks to @mbauman for spotting this issue in #32007 (comment).
1 parent 5b73a97 commit 9c3a15b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,8 @@ function _sparse_findnextnz(m::SparseMatrixCSC, ij::CartesianIndex{2})
13211321
if lo <= n <= hi-1
13221322
return CartesianIndex(m.rowval[n], col)
13231323
end
1324-
nextcol = findnext(c->(c>hi), m.colptr, col+1)
1325-
nextcol === nothing && return nothing
1324+
nextcol = searchsortedfirst(m.colptr, hi + 1, col + 1, length(m.colptr), Base.Order.Forward)
1325+
nextcol > length(m.colptr) && return nothing
13261326
nextlo = m.colptr[nextcol-1]
13271327
return CartesianIndex(m.rowval[nextlo], nextcol - 1)
13281328
end
@@ -1336,8 +1336,8 @@ function _sparse_findprevnz(m::SparseMatrixCSC, ij::CartesianIndex{2})
13361336
if lo <= n <= hi-1
13371337
return CartesianIndex(m.rowval[n], col)
13381338
end
1339-
prevcol = findprev(c->(c<lo), m.colptr, col-1)
1340-
prevcol === nothing && return nothing
1339+
prevcol = searchsortedlast(m.colptr, lo - 1, 1, col - 1, Base.Order.Forward)
1340+
prevcol < 1 && return nothing
13411341
prevhi = m.colptr[prevcol+1]
13421342
return CartesianIndex(m.rowval[prevhi-1], prevcol)
13431343
end

0 commit comments

Comments
 (0)