-
-
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
Faster entity cloning #16717
Open
eugineerd
wants to merge
19
commits into
bevyengine:main
Choose a base branch
from
eugineerd:entity_cloning_faster
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Faster entity cloning #16717
+640
−128
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(for types with ReflectDefault only)
alice-i-cecile
added
A-ECS
Entities, components, systems, and events
C-Performance
A change motivated by improving speed, memory usage or compile times
A-Reflection
Runtime information about types
D-Modest
A "normal" level of difficulty; suitable for simple features or challenging fixes
D-Unsafe
Touches with unsafe code in some way
S-Needs-Review
Needs reviewer attention (from anyone!) to move forward
labels
Dec 8, 2024
Fixed tests for reflect-based cloning paths not actually checking reflect paths (oops) and |
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
A-Reflection
Runtime information about types
C-Performance
A change motivated by improving speed, memory usage or compile times
D-Modest
A "normal" level of difficulty; suitable for simple features or challenging fixes
D-Unsafe
Touches with unsafe code in some way
S-Needs-Review
Needs reviewer attention (from anyone!) to move forward
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
#16132 introduced entity cloning functionality, and while it works and is useful, it can be made faster. This is the promised follow-up to improve performance.
Solution
PREFACE: This is my first time writing
unsafe
in rust and I have only vague idea about what I'm doing. I would encourage reviewers to scrutinizeunsafe
parts in particular.The solution is to use
EntityWorldMut::insert_by_ids
to clone components without archetype moves.To facilitate this,
EntityCloner::clone_entity
now reads all components of the source entity and provides clone handlers with the ability to read component data straight from component storage usingread_source_component
and write to intermediate buffer usingwrite_target_component
.ComponentId
is used to check that requested type corresponds to the type available on source entity.Reflect-based handler is a little trickier to pull of: we only have
&dyn Reflect
and no access to the underlying data. For this reason, only components that haveReflectDefault
would use the fast path; all other components will use previous implementation. The good news is that this is actually only a temporary limitation: once #13432 lands we will be able to use the fast path for allReflect
components without usingDefault
to initialize component data.This PR also introduces
entity_cloning
benchmark to better compare changes between the PR and main, you can see the results in the showcase section.Testing
Showcase
Here's a table demonstrating the improvement:
NOTE: reflect benchmarks are run on components with
ReflectDefault
, for components that do not it is unchanged or slightly worse (due to more allocations) thanmain
.Considerations
Once #10154 is resolved a large part of the functionality in this PR will probably become obsolete. It might still be a little bit faster than using command batching, but the complexity might not be worth it.
Migration Guide
EntityCloner
in component clone handlers is changed toComponentCloneCtx
to better separate data.EntityCloneHandler
from enum to struct and added convenience functions to add default clone and reflect handler more easily.