Skip to content
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

Merged
merged 1 commit into from
Mar 16, 2023

Conversation

mjhostet
Copy link
Contributor

Objective

Entities::get does a manual bounds check of a Vec 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, using Vec::get is shorter and more idiomatic, and potentially faster in debug builds.

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.
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times labels Mar 16, 2023
@alice-i-cecile
Copy link
Member

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.

Copy link
Member

@james7132 james7132 left a 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.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Mar 16, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 16, 2023
Merged via the queue into bevyengine:main with commit f3d6c2d Mar 16, 2023
@mjhostet
Copy link
Contributor Author

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.

I'm happy to benchmark (even though this has merged). But I don't see instructions how to do this in CONTRIBUTING.md or elsewhere, did I miss it?

I took a guess and did:

$ cd benches
$ cargo bench --bench ecs -- --save-baseline before
$ git checkout redundant_bounds_check
$ cargo bench --bench ecs -- --save-baseline after
$ critcmp before after > ~/perf_diff.txt

But please let me know if there's a better way.

perf_diff.txt

@mjhostet mjhostet deleted the redundant_bounds_check branch March 17, 2023 17:17
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
Shfty pushed a commit to shfty-rust/bevy that referenced this pull request Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants