-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Remove redundant bounds check in Entities::get
#8108
Remove redundant bounds check in Entities::get
#8108
Conversation
Rather than manually check `Vec` bounds before taking a reference to that slot using brackets (which will do the same bounds check again), use `Vec::get` to do both operations in one step. Although the redundant bounds check should be optimized away in release mode, using `Vec::get` is shorter and more idiomatic.
Could you run the benchmarks for this before / after? I think this is a good change even if it's performance neutral, but it's always nice to have measurements to report. |
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.
Looking at the generated assembly with all the optimizations on, this indeed doesn't make a change. Rust optimizes out the second bounds check. Though in lower optimization levels this does get rid of an extra check and the panic, and the code is both more idiomatic and readable. LGTM.
I'm happy to benchmark (even though this has merged). But I don't see instructions how to do this in I took a guess and did:
But please let me know if there's a better way. |
Objective
Entities::get
does a manual bounds check of aVec
before taking a reference to that slot using brackets, which does the same bounds check again to see if it needs to panic (which of course it can't). This is wasteful.Solution
Use
Vec::get
to both bounds check and create a reference in one step. Although the redundant bounds check should be optimized away in release mode, usingVec::get
is shorter and more idiomatic, and potentially faster in debug builds.