diff --git a/base/sort.jl b/base/sort.jl index 985e0e8f597f3..00ed8fca59a3d 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -5,7 +5,7 @@ module Sort using Base.Order using Base: copymutable, midpoint, require_one_based_indexing, uinttype, - sub_with_overflow, add_with_overflow, OneTo, BitSigned, BitIntegerType, top_set_bit + sub_with_overflow, add_with_overflow, OneTo, BitSigned, BitIntegerType, top_set_bit, Cartesian import Base: sort, @@ -1131,13 +1131,26 @@ function radix_sort_pass!(t, lo, hi, offset, counts, v, shift, chunk_size) # of bucket 0x00 in t[counts[1]], the next element of bucket 0x00 in t[counts[1]+1], # and the last element of bucket 0x00 in t[counts[2]-1]. - for k in lo:hi + #loop unrolled 4x + k = lo + while k <= hi - 4 + Cartesian.@nexprs 4 _ -> begin + x = v[k] # lookup the element + i = (x >> shift)&mask + 1 # compute its bucket's index for this pass + j = counts[i] # lookup the target index + t[j + offset] = x # put the element where it belongs + counts[i] = j + 1 # increment the target index for the next + k += 1 # ↳ element in this bucket + end + end + while k <= hi x = v[k] # lookup the element i = (x >> shift)&mask + 1 # compute its bucket's index for this pass j = counts[i] # lookup the target index t[j + offset] = x # put the element where it belongs counts[i] = j + 1 # increment the target index for the next - end # ↳ element in this bucket + k += 1 # ↳ element in this bucket + end end end function radix_chunk_size_heuristic(lo::Integer, hi::Integer, bits::Unsigned)