Skip to content

Commit 3d80653

Browse files
LilithHafnerLilith Hafner
authored andcommitted
Fixups for #47383 (fixes runbenchmarks("sort")) (#47822)
* add test demonstrating overflow in countsort * fix overflow in countsort * remove unnecessary type annotations (fixes tests) This fixes the test failure because it allows for automatic conversion. The manual for implementing the AbstractArray interface also does not recomend a type signature for the value arg in setindex!. Co-authored-by: Lilith Hafner <Lilith.Hafner@gmail.com> (cherry picked from commit 965bc7d)
1 parent 0b845b1 commit 3d80653

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

base/sort.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,12 @@ struct WithoutMissingVector{T, U} <: AbstractVector{T}
509509
new{nonmissingtype(eltype(data)), typeof(data)}(data)
510510
end
511511
end
512-
Base.@propagate_inbounds function Base.getindex(v::WithoutMissingVector, i::Integer)
512+
Base.@propagate_inbounds function Base.getindex(v::WithoutMissingVector, i)
513513
out = v.data[i]
514514
@assert !(out isa Missing)
515515
out::eltype(v)
516516
end
517-
Base.@propagate_inbounds function Base.setindex!(v::WithoutMissingVector{T}, x::T, i) where T
517+
Base.@propagate_inbounds function Base.setindex!(v::WithoutMissingVector, x, i)
518518
v.data[i] = x
519519
v
520520
end
@@ -830,7 +830,7 @@ maybe_reverse(o::ForwardOrdering, x) = x
830830
maybe_reverse(o::ReverseOrdering, x) = reverse(x)
831831
function _sort!(v::AbstractVector{<:Integer}, ::CountingSort, o::DirectOrdering, kw)
832832
@getkw lo hi mn mx scratch
833-
range = o === Reverse ? mn-mx : mx-mn
833+
range = maybe_unsigned(o === Reverse ? mn-mx : mx-mn)
834834
offs = 1 - (o === Reverse ? mx : mn)
835835

836836
counts = fill(0, range+1) # TODO use scratch (but be aware of type stability)
@@ -843,7 +843,7 @@ function _sort!(v::AbstractVector{<:Integer}, ::CountingSort, o::DirectOrdering,
843843
lastidx = idx + counts[i] - 1
844844
val = i-offs
845845
for j = idx:lastidx
846-
v[j] = val
846+
v[j] = val isa Unsigned && eltype(v) <: Signed ? signed(val) : val
847847
end
848848
idx = lastidx + 1
849849
end

test/sorting.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ end
765765

766766
@testset "Unions with missing" begin
767767
@test issorted(sort(shuffle!(vcat(fill(missing, 10), rand(Int, 100)))))
768+
@test issorted(sort(vcat(rand(Int8, 600), [missing])))
768769
end
769770

770771
@testset "Specific algorithms" begin
@@ -897,6 +898,7 @@ end
897898
@testset "Count sort near the edge of its range" begin
898899
@test issorted(sort(rand(typemin(Int):typemin(Int)+100, 1000)))
899900
@test issorted(sort(rand(typemax(Int)-100:typemax(Int), 1000)))
901+
@test issorted(sort(rand(Int8, 600)))
900902
end
901903

902904
# This testset is at the end of the file because it is slow.

0 commit comments

Comments
 (0)