diff options
| author | bors <bors@rust-lang.org> | 2025-03-12 00:30:16 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-03-12 00:30:16 +0000 |
| commit | d2b52c5c48ea024fc277feddcc92a60cc92be13c (patch) | |
| tree | 5fdf99e53151706e8d2776ca0ced6645f12e944d /compiler/rustc_index/src/slice.rs | |
| parent | c6251023208f7e9789602376909af8aaf09483b7 (diff) | |
| parent | da0fbc19a5b2710d03c9f2d9ccd6bc572c2b2ffa (diff) | |
| download | rust-d2b52c5c48ea024fc277feddcc92a60cc92be13c.tar.gz rust-d2b52c5c48ea024fc277feddcc92a60cc92be13c.zip | |
Auto merge of #137795 - Jarcho:idx_opt, r=davidtwco
Allow bounds checks when enumerating `IndexSlice` to be elided Without this hint, each loop iteration has to separately bounds check the index. See https://godbolt.org/z/zrfPY4Ten for an example. This is technically a behaviour change, but only in cases where the compiler is going to crash anyways.
Diffstat (limited to 'compiler/rustc_index/src/slice.rs')
| -rw-r--r-- | compiler/rustc_index/src/slice.rs | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/compiler/rustc_index/src/slice.rs b/compiler/rustc_index/src/slice.rs index f17ea9e4b59..0f4f885bb3a 100644 --- a/compiler/rustc_index/src/slice.rs +++ b/compiler/rustc_index/src/slice.rs @@ -65,6 +65,8 @@ impl<I: Idx, T> IndexSlice<I, T> { #[inline] pub fn iter_enumerated(&self) -> impl DoubleEndedIterator<Item = (I, &T)> + ExactSizeIterator { + // Allow the optimizer to elide the bounds checking when creating each index. + let _ = I::new(self.len()); self.raw.iter().enumerate().map(|(n, t)| (I::new(n), t)) } @@ -72,6 +74,8 @@ impl<I: Idx, T> IndexSlice<I, T> { pub fn indices( &self, ) -> impl DoubleEndedIterator<Item = I> + ExactSizeIterator + Clone + 'static { + // Allow the optimizer to elide the bounds checking when creating each index. + let _ = I::new(self.len()); (0..self.len()).map(|n| I::new(n)) } @@ -84,6 +88,8 @@ impl<I: Idx, T> IndexSlice<I, T> { pub fn iter_enumerated_mut( &mut self, ) -> impl DoubleEndedIterator<Item = (I, &mut T)> + ExactSizeIterator { + // Allow the optimizer to elide the bounds checking when creating each index. + let _ = I::new(self.len()); self.raw.iter_mut().enumerate().map(|(n, t)| (I::new(n), t)) } |
