Skip to content

Commit 80799aa

Browse files
committed
Optimize SliceIndex::get impl for RangeInclusive
The check for `self.end() == usize::MAX` can be combined with the `self.end() + 1 > slice.len()` check into `self.end() >= slice.len()`, since `self.end() < slice.len()` implies both `self.end() <= slice.len()` and `self.end() < usize::MAX`.
1 parent 346e5b0 commit 80799aa

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

library/core/src/slice/index.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeFull {
658658
}
659659

660660
/// The methods `index` and `index_mut` panic if:
661-
/// - the end of the range is `usize::MAX` or
662661
/// - the start of the range is greater than the end of the range or
663662
/// - the end of the range is out of bounds.
664663
#[stable(feature = "inclusive_range", since = "1.26.0")]
@@ -668,12 +667,12 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
668667

669668
#[inline]
670669
fn get(self, slice: &[T]) -> Option<&[T]> {
671-
if *self.end() == usize::MAX { None } else { self.into_slice_range().get(slice) }
670+
if *self.end() >= slice.len() { None } else { self.into_slice_range().get(slice) }
672671
}
673672

674673
#[inline]
675674
fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]> {
676-
if *self.end() == usize::MAX { None } else { self.into_slice_range().get_mut(slice) }
675+
if *self.end() >= slice.len() { None } else { self.into_slice_range().get_mut(slice) }
677676
}
678677

679678
#[inline]
@@ -697,7 +696,7 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
697696
start = if exhausted { end } else { start };
698697
if let Some(new_len) = usize::checked_sub(end, start) {
699698
// SAFETY: `self` is checked to be valid and in bounds above.
700-
unsafe { return &*get_offset_len_noubcheck(slice, start, new_len) }
699+
unsafe { return &*get_offset_len_noubcheck(slice, start, new_len) };
701700
}
702701
}
703702
slice_index_fail(start, end, slice.len())
@@ -712,7 +711,7 @@ unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
712711
start = if exhausted { end } else { start };
713712
if let Some(new_len) = usize::checked_sub(end, start) {
714713
// SAFETY: `self` is checked to be valid and in bounds above.
715-
unsafe { return &mut *get_offset_len_mut_noubcheck(slice, start, new_len) }
714+
unsafe { return &mut *get_offset_len_mut_noubcheck(slice, start, new_len) };
716715
}
717716
}
718717
slice_index_fail(start, end, slice.len())

tests/codegen-llvm/slice-range-indexing.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ tests!(RangeTo<usize>, get_range_to, index_range_to);
5757
// CHECK: ret
5858
tests!(RangeFrom<usize>, get_range_from, index_range_from);
5959

60-
// 3 comparisons required: (range.end != usize::MAX) && (range.end < slice.len()) && (range.start <= range.end + 1)
60+
// 2 comparisons required: (range.end < slice.len()) && (range.start <= range.end + 1)
6161
// CHECK-LABEL: @get_range_inclusive
62-
// CHECK-COUNT-3: %{{.+}} = icmp
62+
// CHECK-COUNT-2: %{{.+}} = icmp
6363
// CHECK-NOT: %{{.+}} = icmp
6464
// CHECK: ret
6565

@@ -70,9 +70,9 @@ tests!(RangeFrom<usize>, get_range_from, index_range_from);
7070
// CHECK: ret
7171
tests!(RangeInclusive<usize>, get_range_inclusive, index_range_inclusive);
7272

73-
// 2 comparisons required: (range.end != usize::MAX) && (range.end < slice.len())
73+
// 1 comparison required: (range.end < slice.len())
7474
// CHECK-LABEL: @get_range_to_inclusive
75-
// CHECK-COUNT-2: %{{.+}} = icmp
75+
// CHECK-COUNT-1: %{{.+}} = icmp
7676
// CHECK-NOT: %{{.+}} = icmp
7777
// CHECK: ret
7878

0 commit comments

Comments
 (0)