Skip to content

fix(iter::skip): Optimize next and nth implementations of Skip #96350

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
Aug 15, 2022
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
27 changes: 19 additions & 8 deletions library/core/src/iter/adapters/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,32 @@ where
#[inline]
fn next(&mut self) -> Option<I::Item> {
if unlikely(self.n > 0) {
self.iter.nth(crate::mem::take(&mut self.n) - 1)?;
self.iter.nth(crate::mem::take(&mut self.n))
} else {
self.iter.next()
}
self.iter.next()
}

#[inline]
fn nth(&mut self, n: usize) -> Option<I::Item> {
// Can't just add n + self.n due to overflow.
if self.n > 0 {
let to_skip = self.n;
self.n = 0;
// nth(n) skips n+1
self.iter.nth(to_skip - 1)?;
let skip: usize = crate::mem::take(&mut self.n);
// Checked add to handle overflow case.
let n = match skip.checked_add(n) {
Some(nth) => nth,
None => {
// In case of overflow, load skip value, before loading `n`.
// Because the amount of elements to iterate is beyond `usize::MAX`, this
// is split into two `nth` calls where the `skip` `nth` call is discarded.
self.iter.nth(skip - 1)?;
n
}
};
// Load nth element including skip.
self.iter.nth(n)
} else {
self.iter.nth(n)
}
self.iter.nth(n)
}

#[inline]
Expand Down
31 changes: 31 additions & 0 deletions library/core/tests/iter/adapters/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,34 @@ fn test_skip_non_fused() {
// advance it further. `Unfuse` tests that this doesn't happen by panicking in that scenario.
let _ = non_fused.skip(20).next();
}

#[test]
fn test_skip_non_fused_nth_overflow() {
let non_fused = Unfuse::new(0..10);

// Ensures that calling skip and `nth` where the sum would overflow does not fail for non-fused
// iterators.
let _ = non_fused.skip(20).nth(usize::MAX);
}

#[test]
fn test_skip_overflow_wrapping() {
// Test to ensure even on overflowing on `skip+nth` the correct amount of elements are yielded.
struct WrappingIterator(usize);

impl Iterator for WrappingIterator {
type Item = usize;

fn next(&mut self) -> core::option::Option<Self::Item> {
<Self as Iterator>::nth(self, 0)
}

fn nth(&mut self, nth: usize) -> core::option::Option<Self::Item> {
self.0 = self.0.wrapping_add(nth.wrapping_add(1));
Some(self.0)
}
}

let wrap = WrappingIterator(0);
assert_eq!(wrap.skip(20).nth(usize::MAX), Some(20));
}