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

TrustedRandomAccess specialisation for Iterator::cloned when Item: Copy. #44790

Merged
merged 1 commit into from
Sep 28, 2017

Conversation

clarfonthey
Copy link
Contributor

This should fix #44424. It also provides a potential fix for more iterators using Iterator::cloned.

@rust-highfive
Copy link
Collaborator

r? @sfackler

(rust_highfive has picked a reviewer for you, use r? to override)

@clarfonthey clarfonthey force-pushed the zip_bytes branch 3 times, most recently from 9870913 to 71c62cb Compare September 23, 2017 19:26
This verifies that TrustedRandomAccess has no side effects when the
iterator item implements Copy. This also implements TrustedLen and
TrustedRandomAccess for str::Bytes.
@@ -499,6 +499,18 @@ unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I>
fn may_have_side_effect() -> bool { true }
}

#[doc(hidden)]
unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this specialization required? It seems totally equivalent to the impl above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns false for may_have_side_effect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe may_have_side_effect could be removed and it just assumes everything has side effects. Since some .map().zip() invocations optimize ok it could be suspected that the compiler can remove dead code itself. Unfortunately a closer examination of benchmarks is needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does assume that Clone is simply *self, but it could do weird side-effectful things. I think it's probably reasonable to state that we're allowed to assume that Clone behaves sanely for Copy types, but I don't think that's explicitly documented currently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah great, I missed that.

@bluss
Copy link
Member

bluss commented Sep 24, 2017

may_have_side_effect is a crutch for .zip(): by default it calls .next() on the LHS once more than on the RHS, to discover that the LHS is at its end. The specialization aims to preserve this by default, so that nobody notices anything.

(Edited) This whole crutch is actually needed, for when the input iterators are different lengths.

@sfackler
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Sep 25, 2017

📌 Commit 1c589b7 has been approved by sfackler

@carols10cents carols10cents added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Sep 25, 2017
@bors
Copy link
Contributor

bors commented Sep 28, 2017

⌛ Testing commit 1c589b7 with merge f22b9da...

bors added a commit that referenced this pull request Sep 28, 2017
TrustedRandomAccess specialisation for Iterator::cloned when Item: Copy.

This should fix #44424. It also provides a potential fix for more iterators using `Iterator::cloned`.
@bors
Copy link
Contributor

bors commented Sep 28, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: sfackler
Pushing f22b9da to master...

@kennytm
Copy link
Member

kennytm commented Sep 28, 2017

@bors try- retry rollup

I don't know what bors is doing after the last comment...

screenshot_2017-09-28 22 58 55_ggmryv-or8

@bors bors merged commit 1c589b7 into rust-lang:master Sep 28, 2017
@clarfonthey clarfonthey deleted the zip_bytes branch October 8, 2017 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terrible perfomance of the iterator str::bytes().zip(str::bytes())
8 participants