Skip to content

Commit da0fbc1

Browse files
committed
Allow the optimizer to elide bounds checks when enumerating IndexSlice/IndecVec.
1 parent 2f58193 commit da0fbc1

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎compiler/rustc_index/src/slice.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,17 @@ impl<I: Idx, T> IndexSlice<I, T> {
6565

6666
#[inline]
6767
pubfniter_enumerated(&self) -> implDoubleEndedIterator<Item = (I,&T)> + ExactSizeIterator{
68+
// Allow the optimizer to elide the bounds checking when creating each index.
69+
let _ = I::new(self.len());
6870
self.raw.iter().enumerate().map(|(n, t)| (I::new(n), t))
6971
}
7072

7173
#[inline]
7274
pubfnindices(
7375
&self,
7476
) -> implDoubleEndedIterator<Item = I> + ExactSizeIterator + Clone + 'static{
77+
// Allow the optimizer to elide the bounds checking when creating each index.
78+
let _ = I::new(self.len());
7579
(0..self.len()).map(|n| I::new(n))
7680
}
7781

@@ -84,6 +88,8 @@ impl<I: Idx, T> IndexSlice<I, T> {
8488
pubfniter_enumerated_mut(
8589
&mutself,
8690
) -> implDoubleEndedIterator<Item = (I,&mutT)> + ExactSizeIterator{
91+
// Allow the optimizer to elide the bounds checking when creating each index.
92+
let _ = I::new(self.len());
8793
self.raw.iter_mut().enumerate().map(|(n, t)| (I::new(n), t))
8894
}
8995

‎compiler/rustc_index/src/vec.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ impl<I: Idx, T> IndexVec<I, T> {
9393
/// be allocated only once, with a capacity of at least `n`.)
9494
#[inline]
9595
pubfnfrom_fn_n(func:implFnMut(I) -> T,n:usize) -> Self{
96+
// Allow the optimizer to elide the bounds checking when creating each index.
97+
let _ = I::new(n);
9698
IndexVec::from_raw((0..n).map(I::new).map(func).collect())
9799
}
98100

@@ -128,6 +130,8 @@ impl<I: Idx, T> IndexVec<I, T> {
128130
pubfninto_iter_enumerated(
129131
self,
130132
) -> implDoubleEndedIterator<Item = (I,T)> + ExactSizeIterator{
133+
// Allow the optimizer to elide the bounds checking when creating each index.
134+
let _ = I::new(self.len());
131135
self.raw.into_iter().enumerate().map(|(n, t)| (I::new(n), t))
132136
}
133137

0 commit comments

Comments
 (0)