-
Notifications
You must be signed in to change notification settings - Fork 13.3k
core::slice::Iter
and core::slice::IterMut
could be replaced with safe code
#120438
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
Comments
@HomelikeBrick42 You are welcome to PR your version and we will run it through rustc-perf. |
Bumping a slice requires both incrementing the pointer and decrementing the length. The current approach only needs to increment one. Yes, the current design is complex and full of We generally wouldn't recommend that users write code like that when it can be avoided, but slices and their iterators are very central to Rust so going further than usual tends to be worth it. |
Another place this came up recently: https://rust-lang.zulipchat.com/#narrow/stream/187780-t-compiler.2Fwg-llvm/topic/Communicating.20same-provenance.20to.20LLVM/near/426064260 For ZSTs the current implemenation is actually equivalent to what you wrote, just going through extra steps. (I wish it could just be a union, but that pessimizes it in other ways, thus the tricks you see today.) |
I wanted to repeat this because it's such an important point. Slice iterators are the hottest code in all of Rust, both for compile-time and for run-time. Everyone uses them all over the place, and in most code the densest, most perf-critical parts are loops over slices. Tiny μoptimizations on them are thus worth it in a way that wouldn't be the case in any other rust code. I'm therefore going to close this, because I don't think it's realistically actionable. I'd of course like to have them be entirely safe, and agree with the8472 that most code should prefer a safe version for something like this, but because there are material codegen differences with the approaches (most notably the difference in how many values need to be updated on |
The title says it, i dont see any reason why the current
unsafe
implementation using pointers is necessary when it could be written entirely in safe code without too much hassleThe text was updated successfully, but these errors were encountered: