Skip to content

Commit 067b013

Browse files
authored
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.
1 parent 7fa26ce commit 067b013

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
@@ -563,12 +563,15 @@ function _sort!(v::UnwrappableSubArray, a::SubArrayOptimization, o::Ordering, kw
563563
@getkw lo hi
564564
# @assert v.stride1 == 1
565565
parent = v.parent
566-
if parent isa Array && !(parent isa Vector) && hi - lo < 100
566+
if parent isa Array && !(parent isa Vector) && hi - lo < 100 || !iszero(v.offset1)
567567
# vec(::Array{T, ≠1}) allocates and is therefore somewhat expensive.
568568
# We don't want that for small inputs.
569+
570+
# Additionally, if offset1 is non-zero, then this optimization is incompatible with
571+
# algorithms that track absolute first and last indices (e.g. ScratchQuickSort)
569572
_sort!(v, a.next, o, kw)
570573
else
571-
_sort!(vec(parent), a.next, o, (;kw..., lo = lo + v.offset1, hi = hi + v.offset1))
574+
_sort!(vec(parent), a.next, o, kw)
572575
end
573576
end
574577

test/sorting.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,11 @@ end
11401140
end
11411141
end
11421142

1143+
@testset "partialsort! for UnwrappableSubArray with non-zero offset on 1.11 (#59569)" begin
1144+
a = reshape(6000:-1:1, 1000, :) |> collect;
1145+
@test partialsort!(view(copy(a), :, 6), 500:501) == [500, 501]
1146+
end
1147+
11431148
# This testset is at the end of the file because it is slow.
11441149
@testset "searchsorted" begin
11451150
numTypes = [ Int8, Int16, Int32, Int64, Int128,

0 commit comments

Comments
 (0)