mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-17 06:26:55 +00:00
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.
This commit is contained in:
commit
d2b52c5c48
@ -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))
|
||||
}
|
||||
|
||||
|
@ -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())
|
||||
}
|
||||
|
||||
@ -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))
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user