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