Skip to content

Commit 0973498

Browse files
committed
Optimize std::slice::range
Same reasoning as previous commit.
1 parent 3f40a9d commit 0973498

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

library/core/src/slice/index.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,7 @@ where
907907

908908
ops::Bound::Excluded(&end) if end > len => slice_index_fail(0, end, len),
909909
ops::Bound::Excluded(&end) => end,
910+
910911
ops::Bound::Unbounded => len,
911912
};
912913

@@ -962,19 +963,29 @@ where
962963
{
963964
let len = bounds.end;
964965

965-
let start = match range.start_bound() {
966-
ops::Bound::Included(&start) => start,
967-
ops::Bound::Excluded(start) => start.checked_add(1)?,
968-
ops::Bound::Unbounded => 0,
969-
};
970-
971966
let end = match range.end_bound() {
972-
ops::Bound::Included(end) => end.checked_add(1)?,
967+
ops::Bound::Included(&end) if end >= len => return None,
968+
// Cannot overflow because `end < len` implies `end < usize::MAX`.
969+
ops::Bound::Included(end) => end + 1,
970+
971+
ops::Bound::Excluded(&end) if end > len => return None,
973972
ops::Bound::Excluded(&end) => end,
973+
974974
ops::Bound::Unbounded => len,
975975
};
976976

977-
if start > end || end > len { None } else { Some(ops::Range { start, end }) }
977+
let start = match range.start_bound() {
978+
ops::Bound::Excluded(&start) if start >= end => return None,
979+
// Cannot overflow because `start < end` implies `start < usize::MAX`.
980+
ops::Bound::Excluded(&start) => start + 1,
981+
982+
ops::Bound::Included(&start) if start > end => return None,
983+
ops::Bound::Included(&start) => start,
984+
985+
ops::Bound::Unbounded => 0,
986+
};
987+
988+
Some(ops::Range { start, end })
978989
}
979990

980991
/// Converts a pair of `ops::Bound`s into `ops::Range` without performing any
@@ -1003,21 +1014,27 @@ pub(crate) fn into_range(
10031014
len: usize,
10041015
(start, end): (ops::Bound<usize>, ops::Bound<usize>),
10051016
) -> Option<ops::Range<usize>> {
1006-
use ops::Bound;
1007-
let start = match start {
1008-
Bound::Included(start) => start,
1009-
Bound::Excluded(start) => start.checked_add(1)?,
1010-
Bound::Unbounded => 0,
1011-
};
1012-
10131017
let end = match end {
1014-
Bound::Included(end) => end.checked_add(1)?,
1015-
Bound::Excluded(end) => end,
1016-
Bound::Unbounded => len,
1018+
ops::Bound::Included(end) if end >= len => return None,
1019+
// Cannot overflow because `end < len` implies `end < usize::MAX`.
1020+
ops::Bound::Included(end) => end + 1,
1021+
1022+
ops::Bound::Excluded(end) if end > len => return None,
1023+
ops::Bound::Excluded(end) => end,
1024+
1025+
ops::Bound::Unbounded => len,
10171026
};
10181027

1019-
// Don't bother with checking `start < end` and `end <= len`
1020-
// since these checks are handled by `Range` impls
1028+
let start = match start {
1029+
ops::Bound::Excluded(start) if start >= end => return None,
1030+
// Cannot overflow because `start < end` implies `start < usize::MAX`.
1031+
ops::Bound::Excluded(start) => start + 1,
1032+
1033+
ops::Bound::Included(start) if start > end => return None,
1034+
ops::Bound::Included(start) => start,
1035+
1036+
ops::Bound::Unbounded => 0,
1037+
};
10211038

10221039
Some(start..end)
10231040
}

0 commit comments

Comments
 (0)