bevy/crates
Adam 9bda913e36
Remove redundent information and optimize dynamic allocations in Table (#12929)
# Objective

- fix #12853
- Make `Table::allocate` faster

## Solution
The PR consists of multiple steps:

1) For the component data: create a new data-structure that's similar to
`BlobVec` but doesn't store `len` & `capacity` inside of it: "BlobArray"
(name suggestions welcome)
2) For the `Tick` data: create a new data-structure that's similar to
`ThinSlicePtr` but supports dynamic reallocation: "ThinArrayPtr" (name
suggestions welcome)
3) Create a new data-structure that's very similar to `Column` that
doesn't store `len` & `capacity` inside of it: "ThinColumn"
4) Adjust the `Table` implementation to use `ThinColumn` instead of
`Column`

The result is that only one set of `len` & `capacity` is stored in
`Table`, in `Table::entities`

### Notes Regarding Performance
Apart from shaving off some excess memory in `Table`, the changes have
also brought noteworthy performance improvements:
The previous implementation relied on `Vec::reserve` &
`BlobVec::reserve`, but that redundantly repeated the same if statement
(`capacity` == `len`). Now that check could be made at the `Table` level
because the capacity and length of all the columns are synchronized;
saving N branches per allocation. The result is a respectable
performance improvement per every `Table::reserve` (and subsequently
`Table::allocate`) call.

I'm hesitant to give exact numbers because I don't have a lot of
experience in profiling and benchmarking, but these are the results I
got so far:

*`add_remove_big/table` benchmark after the implementation:*


![after_add_remove_big_table](https://github.com/bevyengine/bevy/assets/46227443/b667da29-1212-4020-8bb0-ec0f15bb5f8a)

*`add_remove_big/table` benchmark in main branch (measured in comparison
to the implementation):*


![main_add_remove_big_table](https://github.com/bevyengine/bevy/assets/46227443/41abb92f-3112-4e01-b935-99696eb2fe58)

*`add_remove_very_big/table` benchmark after the implementation:*


![after_add_remove_very_big](https://github.com/bevyengine/bevy/assets/46227443/f268a155-295b-4f55-ab02-f8a9dcc64fc2)

*`add_remove_very_big/table` benchmark in main branch (measured in
comparison to the implementation):*


![main_add_remove_very_big](https://github.com/bevyengine/bevy/assets/46227443/78b4e3a6-b255-47c9-baee-1a24c25b9aea)

cc @james7132 to verify

---

## Changelog

- New data-structure that's similar to `BlobVec` but doesn't store `len`
& `capacity` inside of it: `BlobArray`
- New data-structure that's similar to `ThinSlicePtr` but supports
dynamic allocation:`ThinArrayPtr`
- New data-structure that's very similar to `Column` that doesn't store
`len` & `capacity` inside of it: `ThinColumn`
- Adjust the `Table` implementation to use `ThinColumn` instead of
`Column`
- New benchmark: `add_remove_very_big` to benchmark the performance of
spawning a lot of entities with a lot of components (15) each

## Migration Guide

`Table` now uses `ThinColumn` instead of `Column`. That means that
methods that previously returned `Column`, will now return `ThinColumn`
instead.

`ThinColumn` has a much more limited and low-level API, but you can
still achieve the same things in `ThinColumn` as you did in `Column`.
For example, instead of calling `Column::get_added_tick`, you'd call
`ThinColumn::get_added_ticks_slice` and index it to get the specific
added tick.

---------

Co-authored-by: James Liu <contact@jamessliu.com>
2024-09-16 22:52:05 +00:00
..
bevy_a11y Reflected traits for resources and components: bevy_a11y (#15192) 2024-09-14 01:43:16 +00:00
bevy_animation Reflect derived traits on all components and resources: bevy_animation (#15209) 2024-09-15 11:58:51 +00:00
bevy_app Unify crate-level preludes (#15080) 2024-09-08 17:10:57 +00:00
bevy_asset Reccomend using AssetPlugin.file_path instead of CARGO_MANIFEST_DIR (#15176) 2024-09-13 16:16:23 +00:00
bevy_audio Reflect derived traits on all components and resources: bevy_audio (#15211) 2024-09-15 14:24:00 +00:00
bevy_color Unify crate-level preludes (#15080) 2024-09-08 17:10:57 +00:00
bevy_core Unify crate-level preludes (#15080) 2024-09-08 17:10:57 +00:00
bevy_core_pipeline Reflect derived traits on all components and resources: bevy_core_pipeline (#15213) 2024-09-15 14:23:41 +00:00
bevy_derive Remove deprecated bevy_dynamic_plugin (#14534) 2024-07-30 15:31:08 +00:00
bevy_dev_tools Split OrthographicProjection::default into 2d & 3d (Adopted) (#15073) 2024-09-09 15:51:28 +00:00
bevy_diagnostic Apply unused_qualifications lint (#14828) 2024-08-21 12:29:33 +00:00
bevy_dylib Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_ecs Remove redundent information and optimize dynamic allocations in Table (#12929) 2024-09-16 22:52:05 +00:00
bevy_encase_derive Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_gilrs Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_gizmos Reflect derived traits on all components and resources: bevy_gizmos (#15217) 2024-09-15 14:41:49 +00:00
bevy_gltf Reflect derived traits on all components and resources: bevy_gltf (#15218) 2024-09-15 14:47:43 +00:00
bevy_hierarchy Reflect derived traits on all components and resources: bevy_hierarchy (#15219) 2024-09-15 15:09:28 +00:00
bevy_input Use of deprecated function in example for ButtonInput (#15221) 2024-09-15 15:22:39 +00:00
bevy_internal Remove all existing system order ambiguities in DefaultPlugins (#15031) 2024-09-03 20:24:34 +00:00
bevy_log Unify crate-level preludes (#15080) 2024-09-08 17:10:57 +00:00
bevy_macro_utils Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_math Fix Capsule2d::sample_interior (#15191) 2024-09-14 02:24:08 +00:00
bevy_mikktspace Fix underflow panic in InitTriInfo (#14893) 2024-08-25 14:13:23 +00:00
bevy_pbr Reflect derived traits on all components and resources: bevy_pbr (#15224) 2024-09-15 16:07:30 +00:00
bevy_picking Reflect derived traits on all components and resources: bevy_picking (#15225) 2024-09-15 16:17:39 +00:00
bevy_ptr Remove redundent information and optimize dynamic allocations in Table (#12929) 2024-09-16 22:52:05 +00:00
bevy_reflect Use FromReflect when extracting entities in dynamic scenes (#15174) 2024-09-15 14:33:39 +00:00
bevy_render Reflect derived traits on all components and resources: bevy_render (#15226) 2024-09-15 17:05:11 +00:00
bevy_scene add allow_all and deny_all methods to DynamicSceneBuilder (#15222) 2024-09-15 15:30:53 +00:00
bevy_sprite Reflect derived traits on all components and resources: bevy_sprite (#15227) 2024-09-15 17:10:53 +00:00
bevy_state Reflect derived traits on all components and resources: bevy_state (#15228) 2024-09-15 17:16:45 +00:00
bevy_tasks Unify crate-level preludes (#15080) 2024-09-08 17:10:57 +00:00
bevy_text Reflect derived traits on all components and resources: bevy_text (#15229) 2024-09-15 17:21:02 +00:00
bevy_time Unify crate-level preludes (#15080) 2024-09-08 17:10:57 +00:00
bevy_transform Reflect derived traits on all components and resources: bevy_transform (#15230) 2024-09-15 18:19:44 +00:00
bevy_ui Reflect derived traits on all components and resources: bevy_ui (#15231) 2024-09-15 17:52:38 +00:00
bevy_utils Remove remnant EntityHash and related types from bevy_utils (#15039) 2024-09-09 15:24:17 +00:00
bevy_window Reflect derived traits on all components and resources: bevy_window (#15233) 2024-09-15 17:49:00 +00:00
bevy_winit Remove ReceivedCharacter (#15126) 2024-09-10 00:22:06 +00:00