-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Specialize Zip::nth for TrustedRandomAccess #48635
Conversation
Makes the bench asked about on URLO 58x faster :)
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
Could you add a test to ensure side effects are executed? fn main() {
let mut a = Vec::new();
let mut b = Vec::new();
let value = (1..7)
.map(|n| {
a.push(n);
n * 10
})
.zip((2..9).map(|n| {
b.push(n * 100);
n * 1000
}))
.skip(1)
.nth(3);
assert_eq!(value, Some((50, 6000)));
assert_eq!(a, vec![1, 2, 3, 4, 5]);
assert_eq!(b, vec![200, 300, 400, 500, 600]);
} |
@kennytm Good call. It looks like |
src/libcore/iter/mod.rs
Outdated
@@ -1094,6 +1107,12 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B> | |||
}) | |||
} | |||
|
|||
#[inline] | |||
default fn nth(&mut self, n: usize) -> Option<Self::Item> | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit: why this {
is on a new line 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that is an excellent question 😅
src/libcore/iter/mod.rs
Outdated
@@ -1174,6 +1193,25 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B> | |||
(len, Some(len)) | |||
} | |||
|
|||
#[inline] | |||
fn nth(&mut self, n: usize) -> Option<Self::Item> | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This also)
Travis passed, so @bors r+ |
📌 Commit 5105fc1 has been approved by |
Specialize Zip::nth for TrustedRandomAccess Fixes the performance difference observed in https://users.rust-lang.org/t/performance-difference-between-iterator-zip-and-skip-order/15743 Before: ``` test iter::bench_skip_then_zip ... bench: 154 ns/iter (+/- 4) test iter::bench_zip_then_skip ... bench: 4,725 ns/iter (+/- 225) ``` After: ``` test iter::bench_skip_then_zip ... bench: 154 ns/iter (+/- 2) test iter::bench_zip_then_skip ... bench: 81 ns/iter (+/- 19) ```
Fixes the performance difference observed in https://users.rust-lang.org/t/performance-difference-between-iterator-zip-and-skip-order/15743
Before:
After: