-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Implement TrustedRandomAccess for slice::{Chunks, ChunksMut, Windows} #47142
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @kennytm (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. |
Thanks! @bors r+ |
📌 Commit 43291c6 has been approved by |
Thanks for the fast review! This is going to fail because of #47113 |
Okay. @bors r- |
For good measure, I'll also add unit tests for zipping the 3 iterators. It seems like this is not covered by any of the existing unit tests yet |
This will now fail until #47113 is merged for real |
☔ The latest upstream changes (presumably #47151) made this pull request unmergeable. Please resolve the merge conflicts. |
For testing if the TrustedRandomAccess implementation works.
rust-lang#47113 renamed the private size field to chunk_size for consistency.
bbb27f4
to
3f29e2b
Compare
Rebased on top of latest master |
src/libcore/slice/mod.rs
Outdated
unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> { | ||
unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] { | ||
let start = i * self.chunk_size; | ||
let end = cmp::min(start + self.chunk_size, self.v.len()); |
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.
There is a potential wraparound here isn't there? i is in bounds but start + size can still wrap around. In particular for [(); !0].chunks((!0)/2 +1)
I think.
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.
It turns out Rust ICES on that array. For those that don't like my mobile-friendly syntax, !0
is the same as usize::max_value()
.
However Rust accepts a vec of this length, and we can demo (playground link)
fn main() {
let v = vec![(); !0];
let iter = v.chunks((!0)/2 +1);
for elt in iter {
println!("Found chunk of len {}", elt.len());
}
}
Which has output:
Found chunk of len 9223372036854775808
Found chunk of len 9223372036854775807
Good point, thanks. This should probably use Will fix in a few hours, thanks for spotting this |
I fixed that now Actually this all seems to have been investigated back then already, so it should all be fine. See top of #47126 (comment) |
I think we're good to go actually |
Some benchmark results, based on the code from #47115 (comment) . Just running a 1920*1080*4 byte array through the normal chunks version. Something like 1.5% difference (the new version is faster), but consistently. So even for things that llvm can't auto-vectorize it seems to be slightly faster. Before
After
|
Thanks again! Just wanna confirm, will the expression |
Yes, but only if you call it with an |
@bors r+ |
📌 Commit b69c124 has been approved by |
…s, r=kennytm Implement TrustedRandomAccess for slice::{Chunks, ChunksMut, Windows} As suggested by @bluss in rust-lang#47115 (comment)
Implement TrustedRandomAccess for slice::{Chunks, ChunksMut, Windows} As suggested by @bluss in #47115 (comment)
☀️ Test successful - status-appveyor, status-travis |
As suggested by @bluss in #47115 (comment)