Skip to content

Commit

Permalink
Remove specialized PartialQuickSort{Int} method
Browse files Browse the repository at this point in the history
This specialized method is actually considerably slower than the one that also
works for `PartialQuickSort{<:OrdinalRange}`. It had been removed before in
9e8fcd3 due to an inference issue, and reintroduced by c41d5a3 (#31632) once
that issue got fixed, but apparently without benchmarks. It turns out that saving
one comparison per iteration isn't worth it. The gain is especially large when
looking for a value among the largest in the array, as the specialized method always
sorted all values lower than the requested one.
  • Loading branch information
nalimilan committed Aug 3, 2020
1 parent 9267bbf commit 3b1e98a
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -616,40 +616,20 @@ function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::MergeSortAlg, o::
return v
end

function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::PartialQuickSort{<:Integer},
function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::PartialQuickSort,
o::Ordering)
@inbounds while lo < hi
hi-lo <= SMALL_THRESHOLD && return sort!(v, lo, hi, SMALL_ALGORITHM, o)
j = partition!(v, lo, hi, o)
if j >= a.k
# we don't need to sort anything bigger than j
hi = j-1
elseif j-lo < hi-j
# recurse on the smaller chunk
# this is necessary to preserve O(log(n))
# stack space in the worst case (rather than O(n))
lo < (j-1) && sort!(v, lo, j-1, a, o)
lo = j+1
else
(j+1) < hi && sort!(v, j+1, hi, a, o)
hi = j-1
end
end
return v
end


function sort!(v::AbstractVector, lo::Integer, hi::Integer, a::PartialQuickSort{T},
o::Ordering) where T<:OrdinalRange
@inbounds while lo < hi
hi-lo <= SMALL_THRESHOLD && return sort!(v, lo, hi, SMALL_ALGORITHM, o)
j = partition!(v, lo, hi, o)

if j <= first(a.k)
lo = j+1
elseif j >= last(a.k)
hi = j-1
else
# recurse on the smaller chunk
# this is necessary to preserve O(log(n))
# stack space in the worst case (rather than O(n))
if j-lo < hi-j
lo < (j-1) && sort!(v, lo, j-1, a, o)
lo = j+1
Expand Down

0 comments on commit 3b1e98a

Please sign in to comment.