diff options
| author | arthurprs <arthurprs@gmail.com> | 2018-01-26 12:49:14 +0100 |
|---|---|---|
| committer | arthurprs <arthurprs@gmail.com> | 2018-01-26 12:49:14 +0100 |
| commit | 4f7109a42482e266410b23fedc9643a6d76e5fa5 (patch) | |
| tree | 8d82492efe8eeb755fd3ddf8509696b7b2a03f10 | |
| parent | 0b56ab0f7b0c5e01611b7ea6a28c77bc09c26275 (diff) | |
| download | rust-4f7109a42482e266410b23fedc9643a6d76e5fa5.tar.gz rust-4f7109a42482e266410b23fedc9643a6d76e5fa5.zip | |
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.
| -rw-r--r-- | src/libcore/slice/mod.rs | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index d088d4e6634..ac70ae583c7 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -1223,7 +1223,8 @@ macro_rules! iterator { P: FnMut(Self::Item) -> bool, { // The addition might panic on overflow - let n = self.len(); + // Use the len of the slice to hint optimizer to remove result index bounds check. + let n = make_slice!(self.ptr, self.end).len(); self.try_fold(0, move |i, x| { if predicate(x) { Err(i) } else { Ok(i + 1) } @@ -1241,7 +1242,8 @@ macro_rules! iterator { { // No need for an overflow check here, because `ExactSizeIterator` // implies that the number of elements fits into a `usize`. - let n = self.len(); + // Use the len of the slice to hint optimizer to remove result index bounds check. + let n = make_slice!(self.ptr, self.end).len(); self.try_rfold(n, move |i, x| { let i = i - 1; if predicate(x) { Err(i) } |
