-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Optimize slice {Chunks,Windows}::nth #138562
base: master
Are you sure you want to change the base?
Conversation
r? @Noratrieb rustbot has assigned @Noratrieb. Use |
Generates branchless code
This avoids generating extra instructions that needlessly modify the slice's pointer
@@ -1429,7 +1429,7 @@ impl<'a, T> DoubleEndedIterator for Windows<'a, T> { | |||
fn nth_back(&mut self, n: usize) -> Option<Self::Item> { | |||
let (end, overflow) = self.v.len().overflowing_sub(n); | |||
if end < self.size.get() || overflow { | |||
self.v = &[]; | |||
self.v = &self.v[..0]; |
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.
I think it would make sense to also add the // cheaper than &[]
comment to all these cases as well, it's not immediately obvious that it is
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, one small comment and then we can merge it. I also renamed your PR to be more accurate
I've noticed that the
nth
functions on slice iters had non-optimized-out bounds checks.The new implementation even generates branchless code.