Skip to content

Commit 4f7109a

Browse files
committed
Use the slice length to hint the optimizer
Using the len of the iterator doesn't give the same result. That's also why we can't generalize it to all TrustedLen iterators.
1 parent 0b56ab0 commit 4f7109a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/libcore/slice/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,8 @@ macro_rules! iterator {
12231223
P: FnMut(Self::Item) -> bool,
12241224
{
12251225
// The addition might panic on overflow
1226-
let n = self.len();
1226+
// Use the len of the slice to hint optimizer to remove result index bounds check.
1227+
let n = make_slice!(self.ptr, self.end).len();
12271228
self.try_fold(0, move |i, x| {
12281229
if predicate(x) { Err(i) }
12291230
else { Ok(i + 1) }
@@ -1241,7 +1242,8 @@ macro_rules! iterator {
12411242
{
12421243
// No need for an overflow check here, because `ExactSizeIterator`
12431244
// implies that the number of elements fits into a `usize`.
1244-
let n = self.len();
1245+
// Use the len of the slice to hint optimizer to remove result index bounds check.
1246+
let n = make_slice!(self.ptr, self.end).len();
12451247
self.try_rfold(n, move |i, x| {
12461248
let i = i - 1;
12471249
if predicate(x) { Err(i) }

0 commit comments

Comments
 (0)