From 4695b82f6b3ce04570e752c2e6e0718d7552ee52 Mon Sep 17 00:00:00 2001 From: vero Date: Mon, 15 Jan 2024 07:51:17 -0800 Subject: [PATCH] Use EntityHashMap whenever possible (#11353) # Objective Fixes #11352 ## Solution - Use `EntityHashMap` instead of `HashMap` --- ## Changelog Changed - Use `EntityHashMap` instead of `HashMap` whenever possible ## Migration Guide TODO --- crates/bevy_gltf/src/loader.rs | 6 +++--- crates/bevy_pbr/src/bundle.rs | 4 ++-- crates/bevy_pbr/src/light.rs | 4 ++-- crates/bevy_pbr/src/render/light.rs | 8 ++++---- crates/bevy_render/src/primitives/mod.rs | 4 ++-- crates/bevy_render/src/view/window/mod.rs | 8 ++++---- crates/bevy_render/src/view/window/screenshot.rs | 4 ++-- crates/bevy_ui/src/layout/mod.rs | 8 ++++---- crates/bevy_winit/src/accessibility.rs | 6 +++--- crates/bevy_winit/src/system.rs | 4 ++-- crates/bevy_winit/src/winit_windows.rs | 4 ++-- 11 files changed, 30 insertions(+), 30 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 19e0f07b6b..06ff8f6872 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -33,7 +33,7 @@ use bevy_scene::Scene; #[cfg(not(target_arch = "wasm32"))] use bevy_tasks::IoTaskPool; use bevy_transform::components::Transform; -use bevy_utils::{HashMap, HashSet}; +use bevy_utils::{EntityHashMap, HashMap, HashSet}; use gltf::{ accessor::Iter, mesh::{util::ReadIndices, Mode}, @@ -586,7 +586,7 @@ async fn load_gltf<'a, 'b, 'c>( let mut err = None; let mut world = World::default(); let mut node_index_to_entity_map = HashMap::new(); - let mut entity_to_skin_index_map = HashMap::new(); + let mut entity_to_skin_index_map = EntityHashMap::default(); let mut scene_load_context = load_context.begin_labeled_asset(); world .spawn(SpatialBundle::INHERITED_IDENTITY) @@ -912,7 +912,7 @@ fn load_node( load_context: &mut LoadContext, settings: &GltfLoaderSettings, node_index_to_entity_map: &mut HashMap, - entity_to_skin_index_map: &mut HashMap, + entity_to_skin_index_map: &mut EntityHashMap, active_camera_found: &mut bool, parent_transform: &Transform, ) -> Result<(), GltfError> { diff --git a/crates/bevy_pbr/src/bundle.rs b/crates/bevy_pbr/src/bundle.rs index 851f87bcd7..6c7829bd12 100644 --- a/crates/bevy_pbr/src/bundle.rs +++ b/crates/bevy_pbr/src/bundle.rs @@ -11,7 +11,7 @@ use bevy_render::{ view::{InheritedVisibility, ViewVisibility, Visibility, VisibleEntities}, }; use bevy_transform::components::{GlobalTransform, Transform}; -use bevy_utils::HashMap; +use bevy_utils::EntityHashMap; /// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`]. pub type PbrBundle = MaterialMeshBundle; @@ -75,7 +75,7 @@ impl CubemapVisibleEntities { pub struct CascadesVisibleEntities { /// Map of view entity to the visible entities for each cascade frustum. #[reflect(ignore)] - pub entities: HashMap>, + pub entities: EntityHashMap>, } /// A component bundle for [`PointLight`] entities. diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index c67f10911a..6e1de192a9 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -16,7 +16,7 @@ use bevy_render::{ view::{InheritedVisibility, RenderLayers, ViewVisibility, VisibleEntities}, }; use bevy_transform::components::{GlobalTransform, Transform}; -use bevy_utils::{tracing::warn, HashMap}; +use bevy_utils::{tracing::warn, EntityHashMap}; use crate::*; @@ -381,7 +381,7 @@ impl From for CascadeShadowConfig { #[reflect(Component)] pub struct Cascades { /// Map from a view to the configuration of each of its [`Cascade`]s. - pub(crate) cascades: HashMap>, + pub(crate) cascades: EntityHashMap>, } #[derive(Clone, Debug, Default, Reflect)] diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index f7666f1205..8221f10ecb 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -18,7 +18,7 @@ use bevy_transform::{components::GlobalTransform, prelude::Transform}; use bevy_utils::{ nonmax::NonMaxU32, tracing::{error, warn}, - HashMap, + EntityHashMap, }; use std::{hash::Hash, num::NonZeroU64, ops::Range}; @@ -47,7 +47,7 @@ pub struct ExtractedDirectionalLight { shadow_depth_bias: f32, shadow_normal_bias: f32, cascade_shadow_config: CascadeShadowConfig, - cascades: HashMap>, + cascades: EntityHashMap>, render_layers: RenderLayers, } @@ -550,7 +550,7 @@ pub const CLUSTERED_FORWARD_STORAGE_BUFFER_COUNT: u32 = 3; #[derive(Resource)] pub struct GlobalLightMeta { pub gpu_point_lights: GpuPointLights, - pub entity_to_index: HashMap, + pub entity_to_index: EntityHashMap, } impl FromWorld for GlobalLightMeta { @@ -567,7 +567,7 @@ impl GlobalLightMeta { pub fn new(buffer_binding_type: BufferBindingType) -> Self { Self { gpu_point_lights: GpuPointLights::new(buffer_binding_type), - entity_to_index: HashMap::default(), + entity_to_index: EntityHashMap::default(), } } } diff --git a/crates/bevy_render/src/primitives/mod.rs b/crates/bevy_render/src/primitives/mod.rs index 8b74959dc6..0e4dfd7dd9 100644 --- a/crates/bevy_render/src/primitives/mod.rs +++ b/crates/bevy_render/src/primitives/mod.rs @@ -3,7 +3,7 @@ use std::borrow::Borrow; use bevy_ecs::{component::Component, prelude::Entity, reflect::ReflectComponent}; use bevy_math::{Affine3A, Mat3A, Mat4, Vec3, Vec3A, Vec4, Vec4Swizzles}; use bevy_reflect::Reflect; -use bevy_utils::HashMap; +use bevy_utils::EntityHashMap; /// An axis-aligned bounding box, defined by: /// - a center, @@ -323,7 +323,7 @@ impl CubemapFrusta { #[reflect(Component)] pub struct CascadesFrusta { #[reflect(ignore)] - pub frusta: HashMap>, + pub frusta: EntityHashMap>, } #[cfg(test)] diff --git a/crates/bevy_render/src/view/window/mod.rs b/crates/bevy_render/src/view/window/mod.rs index aac968940d..aec0e53d1a 100644 --- a/crates/bevy_render/src/view/window/mod.rs +++ b/crates/bevy_render/src/view/window/mod.rs @@ -8,7 +8,7 @@ use crate::{ }; use bevy_app::{App, Plugin}; use bevy_ecs::prelude::*; -use bevy_utils::{default, tracing::debug, HashMap, HashSet}; +use bevy_utils::{default, tracing::debug, EntityHashMap, HashSet}; use bevy_window::{ CompositeAlphaMode, PresentMode, PrimaryWindow, RawHandleWrapper, Window, WindowClosed, }; @@ -89,11 +89,11 @@ impl ExtractedWindow { #[derive(Default, Resource)] pub struct ExtractedWindows { pub primary: Option, - pub windows: HashMap, + pub windows: EntityHashMap, } impl Deref for ExtractedWindows { - type Target = HashMap; + type Target = EntityHashMap; fn deref(&self) -> &Self::Target { &self.windows @@ -199,7 +199,7 @@ struct SurfaceData { #[derive(Resource, Default)] pub struct WindowSurfaces { - surfaces: HashMap, + surfaces: EntityHashMap, /// List of windows that we have already called the initial `configure_surface` for configured_windows: HashSet, } diff --git a/crates/bevy_render/src/view/window/screenshot.rs b/crates/bevy_render/src/view/window/screenshot.rs index d73797e68a..7238df17a7 100644 --- a/crates/bevy_render/src/view/window/screenshot.rs +++ b/crates/bevy_render/src/view/window/screenshot.rs @@ -5,7 +5,7 @@ use bevy_asset::{load_internal_asset, Handle}; use bevy_ecs::prelude::*; use bevy_log::{error, info, info_span}; use bevy_tasks::AsyncComputeTaskPool; -use bevy_utils::HashMap; +use bevy_utils::EntityHashMap; use std::sync::Mutex; use thiserror::Error; use wgpu::{ @@ -33,7 +33,7 @@ pub type ScreenshotFn = Box; #[derive(Resource, Default)] pub struct ScreenshotManager { // this is in a mutex to enable extraction with only an immutable reference - pub(crate) callbacks: Mutex>, + pub(crate) callbacks: Mutex>, } #[derive(Error, Debug)] diff --git a/crates/bevy_ui/src/layout/mod.rs b/crates/bevy_ui/src/layout/mod.rs index 16c3aa1f5b..806896b4d6 100644 --- a/crates/bevy_ui/src/layout/mod.rs +++ b/crates/bevy_ui/src/layout/mod.rs @@ -15,7 +15,7 @@ use bevy_hierarchy::{Children, Parent}; use bevy_log::warn; use bevy_math::Vec2; use bevy_transform::components::Transform; -use bevy_utils::{default, HashMap}; +use bevy_utils::{default, EntityHashMap}; use bevy_window::{PrimaryWindow, Window, WindowResolution, WindowScaleFactorChanged}; use std::fmt; use taffy::Taffy; @@ -50,14 +50,14 @@ struct RootNodePair { #[derive(Resource)] pub struct UiSurface { - entity_to_taffy: HashMap, - window_roots: HashMap>, + entity_to_taffy: EntityHashMap, + window_roots: EntityHashMap>, taffy: Taffy, } fn _assert_send_sync_ui_surface_impl_safe() { fn _assert_send_sync() {} - _assert_send_sync::>(); + _assert_send_sync::>(); _assert_send_sync::(); _assert_send_sync::(); } diff --git a/crates/bevy_winit/src/accessibility.rs b/crates/bevy_winit/src/accessibility.rs index e1ea386af5..633cb15125 100644 --- a/crates/bevy_winit/src/accessibility.rs +++ b/crates/bevy_winit/src/accessibility.rs @@ -22,16 +22,16 @@ use bevy_ecs::{ system::{NonSend, NonSendMut, Query, Res, ResMut, Resource}, }; use bevy_hierarchy::{Children, Parent}; -use bevy_utils::HashMap; +use bevy_utils::EntityHashMap; use bevy_window::{PrimaryWindow, Window, WindowClosed}; /// Maps window entities to their `AccessKit` [`Adapter`]s. #[derive(Default, Deref, DerefMut)] -pub struct AccessKitAdapters(pub HashMap); +pub struct AccessKitAdapters(pub EntityHashMap); /// Maps window entities to their respective [`WinitActionHandler`]s. #[derive(Resource, Default, Deref, DerefMut)] -pub struct WinitActionHandlers(pub HashMap); +pub struct WinitActionHandlers(pub EntityHashMap); /// Forwards `AccessKit` [`ActionRequest`]s from winit to an event channel. #[derive(Clone, Default, Deref, DerefMut)] diff --git a/crates/bevy_winit/src/system.rs b/crates/bevy_winit/src/system.rs index 4ae4b5bd1d..45557b3639 100644 --- a/crates/bevy_winit/src/system.rs +++ b/crates/bevy_winit/src/system.rs @@ -9,7 +9,7 @@ use bevy_ecs::{ }; use bevy_utils::{ tracing::{error, info, warn}, - HashMap, + EntityHashMap, }; use bevy_window::{RawHandleWrapper, Window, WindowClosed, WindowCreated}; use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; @@ -87,7 +87,7 @@ pub(crate) fn create_windows<'a>( /// Cache for closing windows so we can get better debug information. #[derive(Debug, Clone, Resource)] -pub struct WindowTitleCache(HashMap); +pub struct WindowTitleCache(EntityHashMap); pub(crate) fn despawn_windows( mut closed: RemovedComponents, diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 819d170252..be7b3ffac2 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -7,7 +7,7 @@ use bevy_a11y::{ }; use bevy_ecs::entity::Entity; -use bevy_utils::{tracing::warn, HashMap}; +use bevy_utils::{tracing::warn, EntityHashMap, HashMap}; use bevy_window::{CursorGrabMode, Window, WindowMode, WindowPosition, WindowResolution}; use winit::{ @@ -27,7 +27,7 @@ pub struct WinitWindows { /// Stores [`winit`] windows by window identifier. pub windows: HashMap, /// Maps entities to `winit` window identifiers. - pub entity_to_winit: HashMap, + pub entity_to_winit: EntityHashMap, /// Maps `winit` window identifiers to entities. pub winit_to_entity: HashMap, // Many `winit` window functions (e.g. `set_window_icon`) can only be called on the main thread.