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

Use a unstable sort to sort component ids in bevy_ecs #13789

Merged
merged 3 commits into from
Jun 17, 2024

Conversation

Brezak
Copy link
Contributor

@Brezak Brezak commented Jun 10, 2024

Objective

While writing code for the bevy_ecs I noticed we were using a unnecessarily stable sort to sort component ids

Solution

  • Sort component ids with a unstable sort
  • Comb the bevy_ecs crate for any other obvious inefficiencies.
  • Don't clone component vectors when inserting an archetype.

Testing

I ran cargo test -p bevy_ecs. Everything else I leave to CI.

Profiling

I measured about a 1% speed increase when spawning entities directly into a world. Since the difference is so small (and might just be noise) I didn't bother to figure out which of change if any made the biggest difference.

Tracy data Yellow is this PR. Red is the commit I branched from.

image

Methodology I created a system that spawn a 1000 entities each with the same 30 components each frame, and then I measured it's run time. The unusually high number of components was chosen because the standard library [will use a insertion sort for slices under 20 elements](https://github.com/rust-lang/rust/blob/0de24a5177b1d49d6304f76f3ab159faaec134f9/library/core/src/slice/sort.rs#L1048-L1049). This holds for both stable and unstable sorts.

Equal component ids are indistinguishable so there will be no difference
between the results of stable and unstable sorts.
Not a optimization but it's more readable and we're already doing these small changes.
@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 C-Code-Quality A section of code that is hard to understand or change labels Jun 10, 2024
@alice-i-cecile alice-i-cecile added D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Domain-Agnostic Can be tackled by anyone with generic programming or Rust skills S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 10, 2024
@Brezak Brezak changed the title Use a unstable sort to sort compoenent ids in bevy_ecs Use a unstable sort to sort component ids in bevy_ecs Jun 10, 2024
@BD103 BD103 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 15, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 17, 2024
Merged via the queue into bevyengine:main with commit 16e02e1 Jun 17, 2024
35 checks passed
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-Code-Quality A section of code that is hard to understand or change C-Performance A change motivated by improving speed, memory usage or compile times D-Domain-Agnostic Can be tackled by anyone with generic programming or Rust skills D-Straightforward Simple bug fixes and API improvements, docs, test and examples 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.

None yet

3 participants