Skip to content

Commit

Permalink
Explain explicit slicing in slice cmp and partial_cmp methods
Browse files Browse the repository at this point in the history
The explicit slicing is needed in order to enable additional range
check optimizations in the compiler.
  • Loading branch information
ranma42 committed Sep 16, 2015
1 parent 08b9edf commit 74dc146
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,9 @@ impl<T: Eq> Eq for [T] {}
impl<T: Ord> Ord for [T] {
fn cmp(&self, other: &[T]) -> Ordering {
let l = cmp::min(self.len(), other.len());

// Slice to the loop iteration range to enable bound check
// elimination in the compiler
let lhs = &self[..l];
let rhs = &other[..l];

Expand All @@ -1578,6 +1581,9 @@ impl<T: Ord> Ord for [T] {
impl<T: PartialOrd> PartialOrd for [T] {
fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
let l = cmp::min(self.len(), other.len());

// Slice to the loop iteration range to enable bound check
// elimination in the compiler
let lhs = &self[..l];
let rhs = &other[..l];

Expand Down

0 comments on commit 74dc146

Please sign in to comment.