bevy/crates/bevy_ecs/src
James Liu 9eb69282ef Directly copy moved Table components to the target location (#5056)
# Objective
Speed up entity moves between tables by reducing the number of copies conducted. Currently three separate copies are conducted: `src[index] -> swap scratch`, `src[last] -> src[index]`, and `swap scratch -> dst[target]`. The first and last copies can be merged by directly using the copy `src[index] -> dst[target]`, which can save quite some time if the component(s) in question are large.

## Solution
This PR does the  following:

 - Adds `BlobVec::swap_remove_unchecked(usize, PtrMut<'_>)`, which is identical to `swap_remove_and_forget_unchecked`, but skips the `swap_scratch` and directly copies the component into the provided `PtrMut<'_>`.
 - Build `Column::initialize_from_unchecked(&mut Column, usize, usize)` on top of it, which uses the above to directly initialize a row from another column. 
 - Update most of the table move APIs to use `initialize_from_unchecked` instead of a combination of `swap_remove_and_forget_unchecked` and `initialize`.

This is an alternative, though orthogonal, approach to achieve the same performance gains as seen in #4853. This (hopefully) shouldn't run into the same Miri limitations that said PR currently does.  After this PR, `swap_remove_and_forget_unchecked` is still in use for Resources and swap_scratch likely still should be removed, so #4853 still has use, even if this PR is merged.

## Performance
TODO: Microbenchmark

This PR shows similar improvements to commands that add or remove table components that result in a table move. When tested on `many_cubes sphere`, some of the more command heavy systems saw notable improvements. In particular, `prepare_uniform_components<T>`, this saw a reduction in time from 1.35ms to 1.13ms (a 16.3% improvement) on my local machine, a similar if not slightly better gain than what #4853 showed [here](https://github.com/bevyengine/bevy/pull/4853#issuecomment-1159346106).

![image](https://user-images.githubusercontent.com/3137680/174570088-1c4c6fd7-3215-478c-9eb7-8bd9fe486b32.png)

The command heavy `Extract` stage also saw a smaller overall improvement:

![image](https://user-images.githubusercontent.com/3137680/174572261-8a48f004-ab9f-4cb2-b304-a882b6d78065.png)
---

## Changelog
Added: `BlobVec::swap_remove_unchecked`.
Added: `Column::initialize_from_unchecked`.
2022-06-27 16:52:26 +00:00
..
entity Improve entity and component API docs (#4767) 2022-06-21 15:29:22 +00:00
query Directly copy moved Table components to the target location (#5056) 2022-06-27 16:52:26 +00:00
schedule Bugfix State::set transition condition infinite loop (#4890) 2022-06-12 19:34:26 +00:00
storage Directly copy moved Table components to the target location (#5056) 2022-06-27 16:52:26 +00:00
system Replace ReadOnlyFetch with ReadOnlyWorldQuery (#4626) 2022-06-13 23:35:54 +00:00
world Add component_id function to World and Components (#5066) 2022-06-25 20:41:54 +00:00
archetype.rs Mark mutable APIs under ECS storage as pub(crate) (#5065) 2022-06-21 20:35:26 +00:00
bundle.rs Add comparison methods to FilteredAccessSet (#4211) 2022-05-09 14:39:22 +00:00
change_detection.rs untyped APIs for components and resources (#4447) 2022-05-30 15:32:47 +00:00
component.rs Remove double blank line from component docs (#5102) 2022-06-26 14:24:04 +00:00
event.rs Add a clear() method to the EventReader that consumes the iterator (#4693) 2022-05-13 00:57:04 +00:00
lib.rs Add global init and get accessors for all newtyped TaskPools (#2250) 2022-06-09 02:43:24 +00:00
reflect.rs bevy_reflect: put serialize into external ReflectSerialize type (#4782) 2022-06-20 17:18:58 +00:00