Skip to content
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

Zip implementation optimization using TrustedRandomAccess can remove observable side-effects. #85890

Closed
steffahn opened this issue Jun 1, 2021 · 1 comment
Labels
A-iterators Area: Iterators C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented Jun 1, 2021

fn f(x: impl Iterator) {
    let y = x.zip(<[()]>::iter(&[]));
    let mut z = y.zip(<[()]>::iter(&[]));
    z.next();
}

fn main() {
    let x = [1, 2, 3].iter().map(|_| println!("  hi!"));
    println!("first:");
    f(Box::new(x.clone())); // prints "hi!" (Box doesn’t implement TrustedRandomAccess)
    println!("second:");
    f(x.clone()); // doesn’t print "hi!" (Cloned<slice::Iter> implements TrustedRandomAccess)
    println!("done.");
}
first:
  hi!
second:
done.

(playground)

This is a flaw in the current specialized implementation of next().

impl<A, B> ZipImpl<A, B> for Zip<A, B>
where
    A: TrustedRandomAccess + Iterator,
    B: TrustedRandomAccess + Iterator,
{
    #[inline]
    fn next(&mut self) -> Option<(A::Item, B::Item)> {
        if self.index < self.len {
            let i = self.index;
            self.index += 1;
            // SAFETY: `i` is smaller than `self.len`, thus smaller than `self.a.len()` and `self.b.len()`
            unsafe {
                Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
            }
        } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a_len {
            let i = self.index;
            self.index += 1;
            self.len += 1;
            // match the base implementation's potential side effects
            // SAFETY: we just checked that `i` < `self.a.len()`
            unsafe {
                self.a.__iterator_get_unchecked(i);
            }
            None
        } else {
            None
        }
    }}

This implementation makes sure to keep trying to evaluate the first iterator in the Zip like the short-circuiting default implementation

impl<A, B> ZipImpl<A, B> for Zip<A, B>
where
   A: Iterator,
   B: Iterator,
{
    #[inline]
   default fn next(&mut self) -> Option<(A::Item, B::Item)> {
       let x = self.a.next()?;
       let y = self.b.next()?;
       Some((x, y))
   }}

However, the default implementation does it differently: It keeps polling the first iterator even if it’s empty as indicated by its size_hint, whereas the TrustedRandomAccess-using implementation stops as soon as the first iterator is known to be empty based on size calculations.

@rustbot label T-libs-impl, A-iterators

@steffahn steffahn added the C-bug Category: This is a bug. label Jun 1, 2021
@rustbot rustbot added A-iterators Area: Iterators T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 1, 2021
@steffahn
Copy link
Member Author

steffahn commented Jun 1, 2021

Duplicate of #82303

@steffahn steffahn closed this as completed Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-iterators Area: Iterators C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants