bevy/crates
urben1680 546711b807
Split EntityClonerBuilder in OptOut and OptIn variants (#19649)
# Objective

Further tests after #19326 showed that configuring `EntityCloner` with
required components is bug prone and the current design has several
weaknesses in it's API:

- Mixing `EntityClonerBuilder::allow` and `EntityClonerBuilder::deny`
requires extra care how to support that which has an impact on
surrounding code that has to keep edge cases in mind. This is especially
true for attempts to fix the following issues. There is no use-case
known (to me) why someone would mix those.
- A builder with `EntityClonerBuilder::allow_all` configuration tries to
support required components like `EntityClonerBuilder::deny_all` does,
but the meaning of that is conflicting with how you'd expect things to
work:
- If all components should be cloned except component `A`, do you also
want to exclude required components of `A` too? Or are these also valid
without `A` at the target entity?
- If `EntityClonerBuilder::allow_all` should ignore required components
and not add them to be filtered away, which purpose has
`EntityClonerBuilder::without_required_components` for this cloner?
- Other bugs found with the linked PR are:
- Denying `A` also denies required components of `A` even when `A` does
not exist at the source entity
- Allowing `A` also allows required components of `A` even when `A` does
not exist at the source entity
- Adding `allow_if_new` filters to the cloner faces the same issues and
require a common solution to dealing with source-archetype sensitive
cloning

Alternative to #19632 and #19635.

# Solution

`EntityClonerBuilder` is made generic and split into
`EntityClonerBuilder<OptOut>` and `EntityClonerBuilder<OptIn>`

For an overview of the changes, see the migration guide. It is generally
a good idea to start a review of that.

## Algorithm

The generic of `EntityClonerBuilder` contains the filter data that is
needed to build and clone the entity components.

As the filter needs to be borrowed mutably for the duration of the
clone, the borrow checker forced me to separate the filter value and all
other fields in `EntityCloner`. The latter are now in the
`EntityClonerConfig` struct. This caused many changed LOC, sorry.

To make reviewing easier:

1. Check the migration guide
2. Many methods of `EntityCloner` now just call identitcal
`EntityClonerConfig` methods with a mutable borrow of the filter
3. Check `EntityClonerConfig::clone_entity_internal` which changed a bit
regarding the filter usage that is now trait powered (`CloneByFilter`)
to support `OptOut`, `OptIn` and `EntityClonerFilter` (an enum combining
the first two)
4. Check `OptOut` type that no longer tracks required components but has
a `insert_mode` field
5. Check `OptIn` type that has the most logic changes

# Testing

I added a bunch of tests that cover the new logic parts and the fixed
issues.

Benchmarks are in a comment a bit below which shows ~4% to 9%
regressions, but it varied wildly for me. For example at one run the
reflection-based clonings were on-par with main while the other are not,
and redoing that swapped the situation for both.

It would be really cool if I could get some hints how to get better
benchmark results or if you could run them on your machine too.

Just be aware this is not a Performance PR but a Bugfix PR, even if I
smuggled in some more functionalities. So doing changes to
`EntityClonerBuilder` is kind of required here which might make us bite
the bullet.

---------

Co-authored-by: eugineerd <70062110+eugineerd@users.noreply.github.com>
2025-06-24 00:12:08 +00:00
..
bevy_a11y Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_animation Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_anti_aliasing Replace FULLSCREEN_SHADER_HANDLE with a FullscreenShader resource. (#19426) 2025-06-24 00:02:23 +00:00
bevy_app Rename num_entities to entity_count (#19781) 2025-06-23 05:08:02 +00:00
bevy_asset Fix some typos (#19788) 2025-06-23 22:32:46 +00:00
bevy_audio Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_color Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_core_pipeline Replace FULLSCREEN_SHADER_HANDLE with a FullscreenShader resource. (#19426) 2025-06-24 00:02:23 +00:00
bevy_core_widgets Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_derive Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_dev_tools Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_diagnostic Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_dylib Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_ecs Split EntityClonerBuilder in OptOut and OptIn variants (#19649) 2025-06-24 00:12:08 +00:00
bevy_encase_derive Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_gilrs Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_gizmos Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_gltf Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_image Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_input Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_input_focus Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_internal Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_log Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_macro_utils Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_math Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_mesh Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_mikktspace Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_pbr Replace FULLSCREEN_SHADER_HANDLE with a FullscreenShader resource. (#19426) 2025-06-24 00:02:23 +00:00
bevy_picking Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_platform Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_ptr Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_reflect Fix some typos (#19788) 2025-06-23 22:32:46 +00:00
bevy_remote Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_render bevy_solari: RIS for Direct Lighting (#19620) 2025-06-23 00:47:10 +00:00
bevy_scene Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_solari bevy_solari: RIS for Direct Lighting (#19620) 2025-06-23 00:47:10 +00:00
bevy_sprite Add TilemapChunk rendering (#18866) 2025-06-23 23:55:10 +00:00
bevy_state Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_tasks Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_text Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_time Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_transform Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_ui Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_utils Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_window Update doc about Windows CursorGrabMode support (#19192) 2025-06-24 00:00:30 +00:00
bevy_winit Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00