Skip to content

Commit 3d0ac7e

Browse files
committed
Auto merge of rust-lang#92566 - the8472:inline-tra, r=m-ou-se
Inline `__iterator_get_unchecked` for some iterator adapters. This aligns the inline attributes of existing `__iterator_get_unchecked` with those of `next()` on adapters that have both. It improves the performance of iterators using unchecked access when building in incremental mode (due to the larger CGU count?). It might negatively affect incremental compile times for better runtime results, but considering that the equivalent `next()` implementations also are `#[inline]` and usually are more complex this should be ok. ``` ./x.py bench library/core -i --stage 0 --test-args bench_trusted_random_access OLD: 119,172 ns/iter NEW: 17,714 ns/iter ```
2 parents bed05e9 + a68a5d2 commit 3d0ac7e

File tree

6 files changed

+30
-0
lines changed

6 files changed

+30
-0
lines changed

library/core/benches/iter.rs

+24
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,27 @@ fn bench_partial_cmp(b: &mut Bencher) {
367367
fn bench_lt(b: &mut Bencher) {
368368
b.iter(|| (0..100000).map(black_box).lt((0..100000).map(black_box)))
369369
}
370+
371+
#[bench]
372+
fn bench_trusted_random_access_adapters(b: &mut Bencher) {
373+
let vec1: Vec<_> = (0usize..100000).collect();
374+
let vec2 = black_box(vec1.clone());
375+
b.iter(|| {
376+
let mut iter = vec1
377+
.iter()
378+
.copied()
379+
.enumerate()
380+
.map(|(idx, e)| idx.wrapping_add(e))
381+
.zip(vec2.iter().copied())
382+
.map(|(a, b)| a.wrapping_add(b))
383+
.fuse();
384+
let mut acc: usize = 0;
385+
let size = iter.size();
386+
for i in 0..size {
387+
// SAFETY: TRA requirements are satisfied by 0..size iteration and then dropping the
388+
// iterator.
389+
acc = acc.wrapping_add(unsafe { iter.__iterator_get_unchecked(i) });
390+
}
391+
acc
392+
})
393+
}

library/core/benches/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![feature(flt2dec)]
44
#![feature(int_log)]
55
#![feature(test)]
6+
#![feature(trusted_random_access)]
67

78
extern crate test;
89

library/core/src/iter/adapters/enumerate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ where
129129

130130
#[rustc_inherit_overflow_checks]
131131
#[doc(hidden)]
132+
#[inline]
132133
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> <Self as Iterator>::Item
133134
where
134135
Self: TrustedRandomAccessNoCoerce,

library/core/src/iter/adapters/map.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ where
125125
}
126126

127127
#[doc(hidden)]
128+
#[inline]
128129
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> B
129130
where
130131
Self: TrustedRandomAccessNoCoerce,

library/core/src/iter/adapters/zip.rs

+2
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ pub unsafe trait TrustedRandomAccessNoCoerce: Sized {
554554
///
555555
/// Same requirements calling `get_unchecked` directly.
556556
#[doc(hidden)]
557+
#[inline]
557558
pub(in crate::iter::adapters) unsafe fn try_get_unchecked<I>(it: &mut I, idx: usize) -> I::Item
558559
where
559560
I: Iterator,
@@ -576,6 +577,7 @@ unsafe impl<I: Iterator> SpecTrustedRandomAccess for I {
576577
}
577578

578579
unsafe impl<I: Iterator + TrustedRandomAccessNoCoerce> SpecTrustedRandomAccess for I {
580+
#[inline]
579581
unsafe fn try_get_unchecked(&mut self, index: usize) -> Self::Item {
580582
// SAFETY: the caller must uphold the contract for
581583
// `Iterator::__iterator_get_unchecked`.

library/core/src/slice/iter/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ macro_rules! iterator {
326326
}
327327

328328
#[doc(hidden)]
329+
#[inline]
329330
unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
330331
// SAFETY: the caller must guarantee that `i` is in bounds of
331332
// the underlying slice, so `i` cannot overflow an `isize`, and

0 commit comments

Comments
 (0)