Skip to content

Commit b3045a7

Browse files
LilithHafnerKristofferC
authored andcommitted
Only apply Base.Sort.SubArrayOptimization when iszero(v.offset1) (#59572)
Fixes #59569 Thanks @N5N3 for finding the bug! For 1.13, we could re-enable this optimization and propagate the information to the sub-alg or have the sub-alg's embedded indices be relative to `kw.lo` rather than absolute. For backports, this minimal change is more appropriate. (cherry picked from commit 067b013)
1 parent 2bb5165 commit b3045a7

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

base/sort.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,15 @@ function _sort!(v::UnwrappableSubArray, a::SubArrayOptimization, o::Ordering, kw
556556
@getkw lo hi
557557
# @assert v.stride1 == 1
558558
parent = v.parent
559-
if parent isa Array && !(parent isa Vector) && hi - lo < 100
559+
if parent isa Array && !(parent isa Vector) && hi - lo < 100 || !iszero(v.offset1)
560560
# vec(::Array{T, ≠1}) allocates and is therefore somewhat expensive.
561561
# We don't want that for small inputs.
562+
563+
# Additionally, if offset1 is non-zero, then this optimization is incompatible with
564+
# algorithms that track absolute first and last indices (e.g. ScratchQuickSort)
562565
_sort!(v, a.next, o, kw)
563566
else
564-
_sort!(vec(parent), a.next, o, (;kw..., lo = lo + v.offset1, hi = hi + v.offset1))
567+
_sort!(vec(parent), a.next, o, kw)
565568
end
566569
end
567570

test/sorting.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,11 @@ end
10681068
end
10691069
end
10701070

1071+
@testset "partialsort! for UnwrappableSubArray with non-zero offset on 1.11 (#59569)" begin
1072+
a = reshape(6000:-1:1, 1000, :) |> collect;
1073+
@test partialsort!(view(copy(a), :, 6), 500:501) == [500, 501]
1074+
end
1075+
10711076
# This testset is at the end of the file because it is slow.
10721077
@testset "searchsorted" begin
10731078
numTypes = [ Int8, Int16, Int32, Int64, Int128,

0 commit comments

Comments
 (0)