Remove boundary checks in slice comparison operators
In order to get rid of all range checks, the compiler needs to explicitly see that the slices it iterates over are as long as the loop variable upper bound. This further improves the performance of slice comparison: ``` test u8_cmp ... bench: 4,761 ns/iter (+/- 1,203) test u8_lt ... bench: 4,579 ns/iter (+/- 649) test u8_partial_cmp ... bench: 4,768 ns/iter (+/- 761) test u16_cmp ... bench: 4,607 ns/iter (+/- 580) test u16_lt ... bench: 4,681 ns/iter (+/- 567) test u16_partial_cmp ... bench: 4,607 ns/iter (+/- 967) test u32_cmp ... bench: 4,448 ns/iter (+/- 891) test u32_lt ... bench: 4,546 ns/iter (+/- 992) test u32_partial_cmp ... bench: 4,415 ns/iter (+/- 646) test u64_cmp ... bench: 4,380 ns/iter (+/- 1,184) test u64_lt ... bench: 5,684 ns/iter (+/- 602) test u64_partial_cmp ... bench: 4,663 ns/iter (+/- 1,158) ```
This commit is contained in:
@@ -1561,9 +1561,11 @@ impl<T: Ord> Ord for [T] {
|
||||
#[inline]
|
||||
fn cmp(&self, other: &[T]) -> Ordering {
|
||||
let l = cmp::min(self.len(), other.len());
|
||||
let lhs = &self[..l];
|
||||
let rhs = &other[..l];
|
||||
|
||||
for i in 0..l {
|
||||
match self[i].cmp(&other[i]) {
|
||||
match lhs[i].cmp(&rhs[i]) {
|
||||
Ordering::Equal => (),
|
||||
non_eq => return non_eq,
|
||||
}
|
||||
@@ -1578,9 +1580,11 @@ impl<T: PartialOrd> PartialOrd for [T] {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
|
||||
let l = cmp::min(self.len(), other.len());
|
||||
let lhs = &self[..l];
|
||||
let rhs = &other[..l];
|
||||
|
||||
for i in 0..l {
|
||||
match self[i].partial_cmp(&other[i]) {
|
||||
match lhs[i].partial_cmp(&rhs[i]) {
|
||||
Some(Ordering::Equal) => (),
|
||||
non_eq => return non_eq,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user