-
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
Get rid of bounds check in slice::chunks_exact() and related function… #75936
Get rid of bounds check in slice::chunks_exact() and related function… #75936
Conversation
See https://rust.godbolt.org/z/8P74zY for the corresponding assembly of both variants. This is based on the code from #75935, where I noticed this. The variant without bound checks is unrolled by the compiler. Instead of implementing Of course even better would be if LLVM would just understand this itself but that seems considerably harder to achieve. |
b790acf
to
a8cd1de
Compare
Seems like #74938 makes this one here obsolete. Might still be worth merging depending on when an update to LLVM 12 is planned. |
Weird, how come there is no reviewers assigned automatically? @Dylan-DPC |
r? @nagisa |
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.
The only change I would suggest here is to split these inlined algorithms into a private function (split_at_unchecked
/split_at_mut_unchecked
), while also possibly taking advantage of them to implement the split_at
and split_at_mut
as well.
Your call really if you want to do this, r=me otherwise.
@nagisa Would it be good if we have |
Yeah, to implement the Like I said this is not strictly necessary to land this IMO, and even if we don’t make an adjustment here, we can also do the thing in a follow-up. |
I'd be happy to add the |
a8cd1de
to
fd81356
Compare
|
cf86779
to
840819b
Compare
840819b
to
9ad5a85
Compare
/// let (left, right) = v.split_at_unchecked(6); | ||
/// assert!(left == [1, 2, 3, 4, 5, 6]); | ||
/// assert!(right == []); | ||
/// } |
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 we should show an example of invalid use.
…ounds-check, r=nagisa Get rid of bounds check in slice::chunks_exact() and related function… …s during construction LLVM can't figure out in let rem = self.len() % chunk_size; let len = self.len() - rem; let (fst, snd) = self.split_at(len); and let rem = self.len() % chunk_size; let (fst, snd) = self.split_at(rem); that the index passed to split_at() is smaller than the slice length and adds a bounds check plus panic for it. Apart from removing the overhead of the bounds check this also allows LLVM to optimize code around the ChunksExact iterator better.
⌛ Testing commit 8d3cf92 with merge 52d6d94eb07980426e50416d3cf938c5c8335fbb... |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
💔 Test failed - checks-actions |
The CI failure was some kind of network problem:
|
@bors retry |
⌛ Testing commit 8d3cf92 with merge b4b76c287992b99f73159f22a1482866237c9ebd... |
💔 Test failed - checks-actions |
Also looks like an unrelated failure |
@bors retry |
☀️ Test successful - checks-actions, checks-azure |
…r=dtolnay Make `<[T]>::split_at_unchecked` and `<[T]>::split_at_mut_unchecked` public The methods were originally added in rust-lang#75936 (sdroege@30dc32b), but for some reason as private. Nevertheless, the methods have documentation and even a [tracking issue](rust-lang#76014). It's very weird to have a tracking issue for private methods and these methods may be useful outside of the standard library. As such, this PR makes the methods public.
…s during construction
LLVM can't figure out in
and
that the index passed to split_at() is smaller than the slice length and
adds a bounds check plus panic for it.
Apart from removing the overhead of the bounds check this also allows
LLVM to optimize code around the ChunksExact iterator better.