-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 StepBy::nth #47552
Specialize StepBy::nth #47552
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Kimundi (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
src/libcore/iter/mod.rs
Outdated
} | ||
n -= 1; | ||
} | ||
self.iter.nth(n * self.step) |
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.
Good change! ❤️
I think that this needs to handle overflow in the multiplication, though, since I could write (0u64..).step_by(0x10000).nth(0x10000)
on a 32-bit machine, for example.
Also, tests?
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.
Tests are a good call, I found 2 off-by-ones while implementing them.
I added overflow behaviour, but it is pretty weighty. While I think that it shouldn't influence normal execution due to intrinsics::likely
and branch prediction, I do think that it is pretty complicated code, which doesn't really add too much value. Anyway, now that it exists, I don't mind keeping it :)
src/libcore/iter/mod.rs
Outdated
// overflow handling | ||
loop { | ||
let mul = n.checked_mul(step); | ||
if unsafe { intrinsics::likely(mul.is_some())} { |
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.
Nit: space before }
.
Review ping for you @Kimundi! |
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.
Thanks!
@bors r+ |
📌 Commit 4a0da4c has been approved by |
Specialize StepBy::nth This allows optimizations of implementations of the inner iterator's `.nth` method.
I know I'm late. But does anyone else think that this function is rather large to be marked inline? |
@bvinc Well, it's on a generic impl, so it'll be an inlining candidate regardless. And that's good in general, since you really want That said, it might be interesting to split out the overflow handling into a separate function, to make it easier for LLVM to inline the short easy normal path without the hard part. |
This allows optimizations of implementations of the inner iterator's
.nth
method.