mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 12:43:34 +00:00
455c1bfbe8
# Objective Optimize the cloning process for Access-related structs in the ECS system, specifically targeting the `clone_from` method. Previously, profiling showed that 1% of CPU time was spent in `FixedBitSet`'s `drop_in_place`, due to the default `clone_from` implementation: ```rust fn clone_from(&mut self, source: &Self) { *self = source.clone() } ``` This implementation causes unnecessary allocations and deallocations. However, [FixedBitSet provides a more optimized clone_from method](https://github.com/petgraph/fixedbitset/blob/master/src/lib.rs#L1445-L1465) that avoids these allocations and utilizes SIMD instructions for better performance. This PR aims to leverage the optimized clone_from method of FixedBitSet and implement custom clone_from methods for Access-related structs to take full advantage of this optimization. By doing so, we expect to significantly reduce CPU time spent on cloning operations and improve overall system performance. ![image](https://github.com/user-attachments/assets/7526a5c5-c75b-4a9a-b8d2-891f64fd553b) ## Solution - Implemented custom `clone` and `clone_from` methods for `Access`, `FilteredAccess`, `AccessFilters`, and `FilteredAccessSet` structs. - Removed `#[derive(Clone)]` and manually implemented `Clone` trait to use optimized `clone_from` method from `FixedBitSet`. - Added unit tests for cloning and `clone_from` methods to ensure correctness. ## Testing - Conducted performance testing comparing the original and optimized versions. - Measured CPU time consumption for the `clone_from` method: - Original version: 1.34% of CPU time - Optimized version: 0.338% of CPU time - Compared FPS before and after the changes (results may vary depending on the run): Before optimization: ``` 2024-07-28T12:49:11.864019Z INFO bevy diagnostic: fps : 213.489463 (avg 214.502488) 2024-07-28T12:49:11.864037Z INFO bevy diagnostic: frame_time : 4.704746ms (avg 4.682251ms) 2024-07-28T12:49:11.864042Z INFO bevy diagnostic: frame_count: 7947.000000 (avg 7887.500000) ``` ![image](https://github.com/user-attachments/assets/7865a365-0569-4b46-814a-964779d90973) After optimization: ``` 2024-07-28T12:29:42.705738Z INFO bevy diagnostic: fps : 220.273721 (avg 220.912227) 2024-07-28T12:29:42.705762Z INFO bevy diagnostic: frame_time : 4.559127ms (avg 4.544905ms) 2024-07-28T12:29:42.705769Z INFO bevy diagnostic: frame_count: 7596.000000 (avg 7536.500000) ``` ![image](https://github.com/user-attachments/assets/8dd96908-86d0-4850-8e29-f80176a005d6) --- Reviewers can test these changes by running `cargo run --release --example ssr` |
||
---|---|---|
.. | ||
bevy_a11y | ||
bevy_animation | ||
bevy_app | ||
bevy_asset | ||
bevy_audio | ||
bevy_color | ||
bevy_core | ||
bevy_core_pipeline | ||
bevy_derive | ||
bevy_dev_tools | ||
bevy_diagnostic | ||
bevy_dylib | ||
bevy_dynamic_plugin | ||
bevy_ecs | ||
bevy_encase_derive | ||
bevy_gilrs | ||
bevy_gizmos | ||
bevy_gltf | ||
bevy_hierarchy | ||
bevy_input | ||
bevy_internal | ||
bevy_log | ||
bevy_macro_utils | ||
bevy_math | ||
bevy_mikktspace | ||
bevy_pbr | ||
bevy_picking | ||
bevy_ptr | ||
bevy_reflect | ||
bevy_render | ||
bevy_scene | ||
bevy_sprite | ||
bevy_state | ||
bevy_tasks | ||
bevy_text | ||
bevy_time | ||
bevy_transform | ||
bevy_ui | ||
bevy_utils | ||
bevy_window | ||
bevy_winit |