Skip to content

Commit e89b9fd

Browse files
committed
Auto merge of #60772 - timvermeulen:slice_iter_nth_back, r=<try>
Implement nth_back for slice::{Iter, IterMut} Part of #54054. I implemented `nth_back` as straightforwardly as I could, and then slightly changed `nth` to match `nth_back`. I believe I did so correctly, but please double-check 🙂 I also added the helper methods `zst_shrink`, `next_unchecked`, and `next_back_unchecked` to get rid of some duplicated code. These changes hopefully make this code easier to understand for new contributors like me. I noticed the `is_empty!` and `len!` macros which sole purpose seems to be inlining, according to the comment right above them, but the `is_empty` and `len` methods are already marked with `#[inline(always)]`. Does that mean we could replace these macros with method calls, without affecting anything? I'd love to get rid of them.
2 parents a9ec99f + 3834d0e commit e89b9fd

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

src/libcore/slice/mod.rs

+52-19
Original file line numberDiff line numberDiff line change
@@ -3014,21 +3014,41 @@ macro_rules! iterator {
30143014
{$( $mut_:tt )*},
30153015
{$($extra:tt)*}
30163016
) => {
3017+
// Returns the first element and moves the start of the iterator forwards by 1.
3018+
// Greatly improves performance compared to an inlined function. The iterator
3019+
// must not be empty.
3020+
macro_rules! next_unchecked {
3021+
($self: ident) => {& $( $mut_ )* *$self.post_inc_start(1)}
3022+
}
3023+
3024+
// Returns the last element and moves the end of the iterator backwards by 1.
3025+
// Greatly improves performance compared to an inlined function. The iterator
3026+
// must not be empty.
3027+
macro_rules! next_back_unchecked {
3028+
($self: ident) => {& $( $mut_ )* *$self.pre_dec_end(1)}
3029+
}
3030+
30173031
impl<'a, T> $name<'a, T> {
30183032
// Helper function for creating a slice from the iterator.
30193033
#[inline(always)]
30203034
fn make_slice(&self) -> &'a [T] {
30213035
unsafe { from_raw_parts(self.ptr, len!(self)) }
30223036
}
30233037

3038+
// Helper function for shrinking the iterator when T is a ZST, by moving the
3039+
// end of the iterator backwards by `n`.
3040+
#[inline(always)]
3041+
fn zst_shrink(&mut self, n: isize) {
3042+
self.end = (self.end as * $raw_mut u8).wrapping_offset(-n) as * $raw_mut T;
3043+
}
3044+
30243045
// Helper function for moving the start of the iterator forwards by `offset` elements,
30253046
// returning the old start.
30263047
// Unsafe because the offset must be in-bounds or one-past-the-end.
30273048
#[inline(always)]
30283049
unsafe fn post_inc_start(&mut self, offset: isize) -> * $raw_mut T {
30293050
if mem::size_of::<T>() == 0 {
3030-
// This is *reducing* the length. `ptr` never changes with ZST.
3031-
self.end = (self.end as * $raw_mut u8).wrapping_offset(-offset) as * $raw_mut T;
3051+
self.zst_shrink(offset);
30323052
self.ptr
30333053
} else {
30343054
let old = self.ptr;
@@ -3043,7 +3063,7 @@ macro_rules! iterator {
30433063
#[inline(always)]
30443064
unsafe fn pre_dec_end(&mut self, offset: isize) -> * $raw_mut T {
30453065
if mem::size_of::<T>() == 0 {
3046-
self.end = (self.end as * $raw_mut u8).wrapping_offset(-offset) as * $raw_mut T;
3066+
self.zst_shrink(offset);
30473067
self.ptr
30483068
} else {
30493069
self.end = self.end.offset(-offset);
@@ -3080,7 +3100,7 @@ macro_rules! iterator {
30803100
if is_empty!(self) {
30813101
None
30823102
} else {
3083-
Some(& $( $mut_ )* *self.post_inc_start(1))
3103+
Some(next_unchecked!(self))
30843104
}
30853105
}
30863106
}
@@ -3109,11 +3129,10 @@ macro_rules! iterator {
31093129
}
31103130
return None;
31113131
}
3112-
// We are in bounds. `offset` does the right thing even for ZSTs.
3132+
// We are in bounds. `post_inc_start` does the right thing even for ZSTs.
31133133
unsafe {
3114-
let elem = Some(& $( $mut_ )* *self.ptr.add(n));
3115-
self.post_inc_start((n as isize).wrapping_add(1));
3116-
elem
3134+
self.post_inc_start(n as isize);
3135+
Some(next_unchecked!(self))
31173136
}
31183137
}
31193138

@@ -3130,13 +3149,13 @@ macro_rules! iterator {
31303149
let mut accum = init;
31313150
unsafe {
31323151
while len!(self) >= 4 {
3133-
accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3134-
accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3135-
accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3136-
accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3152+
accum = f(accum, next_unchecked!(self))?;
3153+
accum = f(accum, next_unchecked!(self))?;
3154+
accum = f(accum, next_unchecked!(self))?;
3155+
accum = f(accum, next_unchecked!(self))?;
31373156
}
31383157
while !is_empty!(self) {
3139-
accum = f(accum, & $( $mut_ )* *self.post_inc_start(1))?;
3158+
accum = f(accum, next_unchecked!(self))?;
31403159
}
31413160
}
31423161
Try::from_ok(accum)
@@ -3207,11 +3226,25 @@ macro_rules! iterator {
32073226
if is_empty!(self) {
32083227
None
32093228
} else {
3210-
Some(& $( $mut_ )* *self.pre_dec_end(1))
3229+
Some(next_back_unchecked!(self))
32113230
}
32123231
}
32133232
}
32143233

3234+
#[inline]
3235+
fn nth_back(&mut self, n: usize) -> Option<$elem> {
3236+
if n >= len!(self) {
3237+
// This iterator is now empty.
3238+
self.end = self.ptr;
3239+
return None;
3240+
}
3241+
// We are in bounds. `pre_dec_end` does the right thing even for ZSTs.
3242+
unsafe {
3243+
self.pre_dec_end(n as isize);
3244+
Some(next_back_unchecked!(self))
3245+
}
3246+
}
3247+
32153248
#[inline]
32163249
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
32173250
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
@@ -3220,14 +3253,14 @@ macro_rules! iterator {
32203253
let mut accum = init;
32213254
unsafe {
32223255
while len!(self) >= 4 {
3223-
accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3224-
accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3225-
accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3226-
accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3256+
accum = f(accum, next_back_unchecked!(self))?;
3257+
accum = f(accum, next_back_unchecked!(self))?;
3258+
accum = f(accum, next_back_unchecked!(self))?;
3259+
accum = f(accum, next_back_unchecked!(self))?;
32273260
}
32283261
// inlining is_empty everywhere makes a huge performance difference
32293262
while !is_empty!(self) {
3230-
accum = f(accum, & $( $mut_ )* *self.pre_dec_end(1))?;
3263+
accum = f(accum, next_back_unchecked!(self))?;
32313264
}
32323265
}
32333266
Try::from_ok(accum)

src/libcore/tests/slice.rs

+13
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ fn test_iterator_nth() {
8888
assert_eq!(iter.nth(1).unwrap(), &v[4]);
8989
}
9090

91+
#[test]
92+
fn test_iterator_nth_back() {
93+
let v: &[_] = &[0, 1, 2, 3, 4];
94+
for i in 0..v.len() {
95+
assert_eq!(v.iter().nth_back(i).unwrap(), &v[v.len() - i - 1]);
96+
}
97+
assert_eq!(v.iter().nth_back(v.len()), None);
98+
99+
let mut iter = v.iter();
100+
assert_eq!(iter.nth_back(2).unwrap(), &v[2]);
101+
assert_eq!(iter.nth_back(1).unwrap(), &v[0]);
102+
}
103+
91104
#[test]
92105
fn test_iterator_last() {
93106
let v: &[_] = &[0, 1, 2, 3, 4];

0 commit comments

Comments
 (0)