Skip to content

Allow bounds checks when enumerating IndexSlice to be elided #137795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_index/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ 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))
}

#[inline]
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))
}

Expand All @@ -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))
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_index/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl<I: Idx, T> IndexVec<I, T> {
/// be allocated only once, with a capacity of at least `n`.)
#[inline]
pub fn from_fn_n(func: impl FnMut(I) -> T, n: usize) -> Self {
// Allow the optimizer to elide the bounds checking when creating each index.
let _ = I::new(n);
IndexVec::from_raw((0..n).map(I::new).map(func).collect())
}

Expand Down Expand Up @@ -128,6 +130,8 @@ impl<I: Idx, T> IndexVec<I, T> {
pub fn into_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.into_iter().enumerate().map(|(n, t)| (I::new(n), t))
}

Expand Down
Loading