1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -4093,7 +4093,23 @@ impl<T> [T] {
4093
4093
where
4094
4094
T : PartialOrd ,
4095
4095
{
4096
- self . is_sorted_by ( |a, b| a <= b)
4096
+ // This odd number works the best. 32 + 1 extra due to overlapping chunk boundaries.
4097
+ const CHUNK_SIZE : usize = 33 ;
4098
+ if self . len ( ) < CHUNK_SIZE {
4099
+ return self . windows ( 2 ) . all ( |w| w[ 0 ] <= w[ 1 ] ) ;
4100
+ }
4101
+ let mut i = 0 ;
4102
+ // Check in chunks for autovectorization.
4103
+ while i < self . len ( ) - CHUNK_SIZE {
4104
+ let chunk = & self [ i..i + CHUNK_SIZE ] ;
4105
+ if !chunk. windows ( 2 ) . fold ( true , |acc, w| acc & ( w[ 0 ] <= w[ 1 ] ) ) {
4106
+ return false ;
4107
+ }
4108
+ // We need to ensure that chunk boundaries are also sorted.
4109
+ // Overlap the next chunk with the last element of our last chunk.
4110
+ i += CHUNK_SIZE - 1 ;
4111
+ }
4112
+ self [ i..] . windows ( 2 ) . all ( |w| w[ 0 ] <= w[ 1 ] )
4097
4113
}
4098
4114
4099
4115
/// Checks if the elements of this slice are sorted using the given comparator function.
0 commit comments