Skip to content

Commit 4695b82

Browse files
authored
Use EntityHashMap whenever possible (#11353)
# Objective Fixes #11352 ## Solution - Use `EntityHashMap<Entity, T>` instead of `HashMap<Entity, T>` --- ## Changelog Changed - Use `EntityHashMap<Entity, T>` instead of `HashMap<Entity, T>` whenever possible ## Migration Guide TODO
1 parent 3d628a8 commit 4695b82

File tree

11 files changed

+30
-30
lines changed

11 files changed

+30
-30
lines changed

crates/bevy_gltf/src/loader.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use bevy_scene::Scene;
3333
#[cfg(not(target_arch = "wasm32"))]
3434
use bevy_tasks::IoTaskPool;
3535
use bevy_transform::components::Transform;
36-
use bevy_utils::{HashMap, HashSet};
36+
use bevy_utils::{EntityHashMap, HashMap, HashSet};
3737
use gltf::{
3838
accessor::Iter,
3939
mesh::{util::ReadIndices, Mode},
@@ -586,7 +586,7 @@ async fn load_gltf<'a, 'b, 'c>(
586586
let mut err = None;
587587
let mut world = World::default();
588588
let mut node_index_to_entity_map = HashMap::new();
589-
let mut entity_to_skin_index_map = HashMap::new();
589+
let mut entity_to_skin_index_map = EntityHashMap::default();
590590
let mut scene_load_context = load_context.begin_labeled_asset();
591591
world
592592
.spawn(SpatialBundle::INHERITED_IDENTITY)
@@ -912,7 +912,7 @@ fn load_node(
912912
load_context: &mut LoadContext,
913913
settings: &GltfLoaderSettings,
914914
node_index_to_entity_map: &mut HashMap<usize, Entity>,
915-
entity_to_skin_index_map: &mut HashMap<Entity, usize>,
915+
entity_to_skin_index_map: &mut EntityHashMap<Entity, usize>,
916916
active_camera_found: &mut bool,
917917
parent_transform: &Transform,
918918
) -> Result<(), GltfError> {

crates/bevy_pbr/src/bundle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bevy_render::{
1111
view::{InheritedVisibility, ViewVisibility, Visibility, VisibleEntities},
1212
};
1313
use bevy_transform::components::{GlobalTransform, Transform};
14-
use bevy_utils::HashMap;
14+
use bevy_utils::EntityHashMap;
1515

1616
/// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`].
1717
pub type PbrBundle = MaterialMeshBundle<StandardMaterial>;
@@ -75,7 +75,7 @@ impl CubemapVisibleEntities {
7575
pub struct CascadesVisibleEntities {
7676
/// Map of view entity to the visible entities for each cascade frustum.
7777
#[reflect(ignore)]
78-
pub entities: HashMap<Entity, Vec<VisibleEntities>>,
78+
pub entities: EntityHashMap<Entity, Vec<VisibleEntities>>,
7979
}
8080

8181
/// A component bundle for [`PointLight`] entities.

crates/bevy_pbr/src/light.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bevy_render::{
1616
view::{InheritedVisibility, RenderLayers, ViewVisibility, VisibleEntities},
1717
};
1818
use bevy_transform::components::{GlobalTransform, Transform};
19-
use bevy_utils::{tracing::warn, HashMap};
19+
use bevy_utils::{tracing::warn, EntityHashMap};
2020

2121
use crate::*;
2222

@@ -381,7 +381,7 @@ impl From<CascadeShadowConfigBuilder> for CascadeShadowConfig {
381381
#[reflect(Component)]
382382
pub struct Cascades {
383383
/// Map from a view to the configuration of each of its [`Cascade`]s.
384-
pub(crate) cascades: HashMap<Entity, Vec<Cascade>>,
384+
pub(crate) cascades: EntityHashMap<Entity, Vec<Cascade>>,
385385
}
386386

387387
#[derive(Clone, Debug, Default, Reflect)]

crates/bevy_pbr/src/render/light.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use bevy_transform::{components::GlobalTransform, prelude::Transform};
1818
use bevy_utils::{
1919
nonmax::NonMaxU32,
2020
tracing::{error, warn},
21-
HashMap,
21+
EntityHashMap,
2222
};
2323
use std::{hash::Hash, num::NonZeroU64, ops::Range};
2424

@@ -47,7 +47,7 @@ pub struct ExtractedDirectionalLight {
4747
shadow_depth_bias: f32,
4848
shadow_normal_bias: f32,
4949
cascade_shadow_config: CascadeShadowConfig,
50-
cascades: HashMap<Entity, Vec<Cascade>>,
50+
cascades: EntityHashMap<Entity, Vec<Cascade>>,
5151
render_layers: RenderLayers,
5252
}
5353

@@ -550,7 +550,7 @@ pub const CLUSTERED_FORWARD_STORAGE_BUFFER_COUNT: u32 = 3;
550550
#[derive(Resource)]
551551
pub struct GlobalLightMeta {
552552
pub gpu_point_lights: GpuPointLights,
553-
pub entity_to_index: HashMap<Entity, usize>,
553+
pub entity_to_index: EntityHashMap<Entity, usize>,
554554
}
555555

556556
impl FromWorld for GlobalLightMeta {
@@ -567,7 +567,7 @@ impl GlobalLightMeta {
567567
pub fn new(buffer_binding_type: BufferBindingType) -> Self {
568568
Self {
569569
gpu_point_lights: GpuPointLights::new(buffer_binding_type),
570-
entity_to_index: HashMap::default(),
570+
entity_to_index: EntityHashMap::default(),
571571
}
572572
}
573573
}

crates/bevy_render/src/primitives/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::borrow::Borrow;
33
use bevy_ecs::{component::Component, prelude::Entity, reflect::ReflectComponent};
44
use bevy_math::{Affine3A, Mat3A, Mat4, Vec3, Vec3A, Vec4, Vec4Swizzles};
55
use bevy_reflect::Reflect;
6-
use bevy_utils::HashMap;
6+
use bevy_utils::EntityHashMap;
77

88
/// An axis-aligned bounding box, defined by:
99
/// - a center,
@@ -323,7 +323,7 @@ impl CubemapFrusta {
323323
#[reflect(Component)]
324324
pub struct CascadesFrusta {
325325
#[reflect(ignore)]
326-
pub frusta: HashMap<Entity, Vec<Frustum>>,
326+
pub frusta: EntityHashMap<Entity, Vec<Frustum>>,
327327
}
328328

329329
#[cfg(test)]

crates/bevy_render/src/view/window/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
};
99
use bevy_app::{App, Plugin};
1010
use bevy_ecs::prelude::*;
11-
use bevy_utils::{default, tracing::debug, HashMap, HashSet};
11+
use bevy_utils::{default, tracing::debug, EntityHashMap, HashSet};
1212
use bevy_window::{
1313
CompositeAlphaMode, PresentMode, PrimaryWindow, RawHandleWrapper, Window, WindowClosed,
1414
};
@@ -89,11 +89,11 @@ impl ExtractedWindow {
8989
#[derive(Default, Resource)]
9090
pub struct ExtractedWindows {
9191
pub primary: Option<Entity>,
92-
pub windows: HashMap<Entity, ExtractedWindow>,
92+
pub windows: EntityHashMap<Entity, ExtractedWindow>,
9393
}
9494

9595
impl Deref for ExtractedWindows {
96-
type Target = HashMap<Entity, ExtractedWindow>;
96+
type Target = EntityHashMap<Entity, ExtractedWindow>;
9797

9898
fn deref(&self) -> &Self::Target {
9999
&self.windows
@@ -199,7 +199,7 @@ struct SurfaceData {
199199

200200
#[derive(Resource, Default)]
201201
pub struct WindowSurfaces {
202-
surfaces: HashMap<Entity, SurfaceData>,
202+
surfaces: EntityHashMap<Entity, SurfaceData>,
203203
/// List of windows that we have already called the initial `configure_surface` for
204204
configured_windows: HashSet<Entity>,
205205
}

crates/bevy_render/src/view/window/screenshot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy_asset::{load_internal_asset, Handle};
55
use bevy_ecs::prelude::*;
66
use bevy_log::{error, info, info_span};
77
use bevy_tasks::AsyncComputeTaskPool;
8-
use bevy_utils::HashMap;
8+
use bevy_utils::EntityHashMap;
99
use std::sync::Mutex;
1010
use thiserror::Error;
1111
use wgpu::{
@@ -33,7 +33,7 @@ pub type ScreenshotFn = Box<dyn FnOnce(Image) + Send + Sync>;
3333
#[derive(Resource, Default)]
3434
pub struct ScreenshotManager {
3535
// this is in a mutex to enable extraction with only an immutable reference
36-
pub(crate) callbacks: Mutex<HashMap<Entity, ScreenshotFn>>,
36+
pub(crate) callbacks: Mutex<EntityHashMap<Entity, ScreenshotFn>>,
3737
}
3838

3939
#[derive(Error, Debug)]

crates/bevy_ui/src/layout/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bevy_hierarchy::{Children, Parent};
1515
use bevy_log::warn;
1616
use bevy_math::Vec2;
1717
use bevy_transform::components::Transform;
18-
use bevy_utils::{default, HashMap};
18+
use bevy_utils::{default, EntityHashMap};
1919
use bevy_window::{PrimaryWindow, Window, WindowResolution, WindowScaleFactorChanged};
2020
use std::fmt;
2121
use taffy::Taffy;
@@ -50,14 +50,14 @@ struct RootNodePair {
5050

5151
#[derive(Resource)]
5252
pub struct UiSurface {
53-
entity_to_taffy: HashMap<Entity, taffy::node::Node>,
54-
window_roots: HashMap<Entity, Vec<RootNodePair>>,
53+
entity_to_taffy: EntityHashMap<Entity, taffy::node::Node>,
54+
window_roots: EntityHashMap<Entity, Vec<RootNodePair>>,
5555
taffy: Taffy,
5656
}
5757

5858
fn _assert_send_sync_ui_surface_impl_safe() {
5959
fn _assert_send_sync<T: Send + Sync>() {}
60-
_assert_send_sync::<HashMap<Entity, taffy::node::Node>>();
60+
_assert_send_sync::<EntityHashMap<Entity, taffy::node::Node>>();
6161
_assert_send_sync::<Taffy>();
6262
_assert_send_sync::<UiSurface>();
6363
}

crates/bevy_winit/src/accessibility.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ use bevy_ecs::{
2222
system::{NonSend, NonSendMut, Query, Res, ResMut, Resource},
2323
};
2424
use bevy_hierarchy::{Children, Parent};
25-
use bevy_utils::HashMap;
25+
use bevy_utils::EntityHashMap;
2626
use bevy_window::{PrimaryWindow, Window, WindowClosed};
2727

2828
/// Maps window entities to their `AccessKit` [`Adapter`]s.
2929
#[derive(Default, Deref, DerefMut)]
30-
pub struct AccessKitAdapters(pub HashMap<Entity, Adapter>);
30+
pub struct AccessKitAdapters(pub EntityHashMap<Entity, Adapter>);
3131

3232
/// Maps window entities to their respective [`WinitActionHandler`]s.
3333
#[derive(Resource, Default, Deref, DerefMut)]
34-
pub struct WinitActionHandlers(pub HashMap<Entity, WinitActionHandler>);
34+
pub struct WinitActionHandlers(pub EntityHashMap<Entity, WinitActionHandler>);
3535

3636
/// Forwards `AccessKit` [`ActionRequest`]s from winit to an event channel.
3737
#[derive(Clone, Default, Deref, DerefMut)]

crates/bevy_winit/src/system.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bevy_ecs::{
99
};
1010
use bevy_utils::{
1111
tracing::{error, info, warn},
12-
HashMap,
12+
EntityHashMap,
1313
};
1414
use bevy_window::{RawHandleWrapper, Window, WindowClosed, WindowCreated};
1515
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
@@ -87,7 +87,7 @@ pub(crate) fn create_windows<'a>(
8787

8888
/// Cache for closing windows so we can get better debug information.
8989
#[derive(Debug, Clone, Resource)]
90-
pub struct WindowTitleCache(HashMap<Entity, String>);
90+
pub struct WindowTitleCache(EntityHashMap<Entity, String>);
9191

9292
pub(crate) fn despawn_windows(
9393
mut closed: RemovedComponents<Window>,

0 commit comments

Comments
 (0)