Skip to content

Commit e939d6c

Browse files
authored
Remove remnant EntityHash and related types from bevy_utils (#15039)
# Objective `EntityHash` and related types were moved from `bevy_utils` to `bevy_ecs` in #11498, but seemed to have been accidentally reintroduced a week later in #11707. ## Solution Remove the old leftover code. --- ## Migration Guide - Uses of `bevy::utils::{EntityHash, EntityHasher, EntityHashMap, EntityHashSet}` now have to be imported from `bevy::ecs::entity`.
1 parent a9d2a9e commit e939d6c

File tree

6 files changed

+14
-94
lines changed

6 files changed

+14
-94
lines changed

crates/bevy_ecs/src/observer/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ mod trigger_event;
77
pub use runner::*;
88
pub use trigger_event::*;
99

10+
use crate::entity::EntityHashMap;
1011
use crate::observer::entity_observer::ObservedBy;
1112
use crate::{archetype::ArchetypeFlags, system::IntoObserverSystem, world::*};
1213
use crate::{component::ComponentId, prelude::*, world::DeferredWorld};
1314
use bevy_ptr::Ptr;
14-
use bevy_utils::{EntityHashMap, HashMap};
15+
use bevy_utils::HashMap;
1516
use std::{fmt::Debug, marker::PhantomData};
1617

1718
/// Type containing triggered [`Event`] information for a given run of an [`Observer`]. This contains the
@@ -152,15 +153,15 @@ pub struct ObserverTrigger {
152153
}
153154

154155
// Map between an observer entity and its runner
155-
type ObserverMap = EntityHashMap<Entity, ObserverRunner>;
156+
type ObserverMap = EntityHashMap<ObserverRunner>;
156157

157158
/// Collection of [`ObserverRunner`] for [`Observer`] registered to a particular trigger targeted at a specific component.
158159
#[derive(Default, Debug)]
159160
pub struct CachedComponentObservers {
160161
// Observers listening to triggers targeting this component
161162
map: ObserverMap,
162163
// Observers listening to triggers targeting this component on a specific entity
163-
entity_map: EntityHashMap<Entity, ObserverMap>,
164+
entity_map: EntityHashMap<ObserverMap>,
164165
}
165166

166167
/// Collection of [`ObserverRunner`] for [`Observer`] registered to a particular trigger.
@@ -171,7 +172,7 @@ pub struct CachedObservers {
171172
// Observers listening for this trigger fired at a specific component
172173
component_observers: HashMap<ComponentId, CachedComponentObservers>,
173174
// Observers listening for this trigger fired at a specific entity
174-
entity_observers: EntityHashMap<Entity, ObserverMap>,
175+
entity_observers: EntityHashMap<ObserverMap>,
175176
}
176177

177178
/// Metadata for observers. Stores a cache mapping trigger ids to the registered observers.

crates/bevy_reflect/src/impls/std.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,6 @@ macro_rules! impl_reflect_for_hashset {
10291029
}
10301030

10311031
impl_type_path!(::bevy_utils::NoOpHash);
1032-
impl_type_path!(::bevy_utils::EntityHash);
10331032
impl_type_path!(::bevy_utils::FixedState);
10341033

10351034
impl_reflect_for_hashset!(::std::collections::HashSet<V,S>);

crates/bevy_render/src/batching/gpu_preprocessing.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
use bevy_app::{App, Plugin};
44
use bevy_derive::{Deref, DerefMut};
55
use bevy_ecs::{
6-
entity::Entity,
6+
entity::{Entity, EntityHashMap},
77
query::{Has, With},
88
schedule::IntoSystemConfigs as _,
99
system::{Query, Res, ResMut, Resource, StaticSystemParam},
1010
world::{FromWorld, World},
1111
};
1212
use bevy_encase_derive::ShaderType;
13-
use bevy_utils::EntityHashMap;
1413
use bytemuck::{Pod, Zeroable};
1514
use nonmax::NonMaxU32;
1615
use smallvec::smallvec;
@@ -99,7 +98,7 @@ where
9998
/// corresponds to each instance.
10099
///
101100
/// This is keyed off each view. Each view has a separate buffer.
102-
pub work_item_buffers: EntityHashMap<Entity, PreprocessWorkItemBuffer>,
101+
pub work_item_buffers: EntityHashMap<PreprocessWorkItemBuffer>,
103102

104103
/// The uniform data inputs for the current frame.
105104
///

crates/bevy_render/src/view/visibility/range.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ use std::{
99
use bevy_app::{App, Plugin, PostUpdate};
1010
use bevy_ecs::{
1111
component::Component,
12-
entity::Entity,
12+
entity::{Entity, EntityHashMap},
1313
query::{Changed, With},
1414
schedule::IntoSystemConfigs as _,
1515
system::{Query, Res, ResMut, Resource},
1616
};
1717
use bevy_math::{vec4, FloatOrd, Vec4};
1818
use bevy_reflect::Reflect;
1919
use bevy_transform::components::GlobalTransform;
20-
use bevy_utils::{prelude::default, EntityHashMap, HashMap};
20+
use bevy_utils::{prelude::default, HashMap};
2121
use nonmax::NonMaxU16;
2222
use wgpu::{BufferBindingType, BufferUsages};
2323

@@ -191,7 +191,7 @@ impl VisibilityRange {
191191
#[derive(Resource)]
192192
pub struct RenderVisibilityRanges {
193193
/// Information corresponding to each entity.
194-
entities: EntityHashMap<Entity, RenderVisibilityEntityInfo>,
194+
entities: EntityHashMap<RenderVisibilityEntityInfo>,
195195

196196
/// Maps a [`VisibilityRange`] to its index within the `buffer`.
197197
///
@@ -309,13 +309,13 @@ impl RenderVisibilityRanges {
309309
#[derive(Resource, Default)]
310310
pub struct VisibleEntityRanges {
311311
/// Stores which bit index each view corresponds to.
312-
views: EntityHashMap<Entity, u8>,
312+
views: EntityHashMap<u8>,
313313

314314
/// Stores a bitmask in which each view has a single bit.
315315
///
316316
/// A 0 bit for a view corresponds to "out of range"; a 1 bit corresponds to
317317
/// "in range".
318-
entities: EntityHashMap<Entity, u32>,
318+
entities: EntityHashMap<u32>,
319319
}
320320

321321
impl VisibleEntityRanges {

crates/bevy_utils/src/lib.rs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -276,85 +276,6 @@ impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K,
276276
}
277277
}
278278

279-
/// A [`BuildHasher`] that results in a [`EntityHasher`].
280-
#[derive(Default, Clone)]
281-
pub struct EntityHash;
282-
283-
impl BuildHasher for EntityHash {
284-
type Hasher = EntityHasher;
285-
286-
fn build_hasher(&self) -> Self::Hasher {
287-
EntityHasher::default()
288-
}
289-
}
290-
291-
/// A very fast hash that is only designed to work on generational indices
292-
/// like `Entity`. It will panic if attempting to hash a type containing
293-
/// non-u64 fields.
294-
///
295-
/// This is heavily optimized for typical cases, where you have mostly live
296-
/// entities, and works particularly well for contiguous indices.
297-
///
298-
/// If you have an unusual case -- say all your indices are multiples of 256
299-
/// or most of the entities are dead generations -- then you might want also to
300-
/// try [`AHasher`] for a slower hash computation but fewer lookup conflicts.
301-
#[derive(Debug, Default)]
302-
pub struct EntityHasher {
303-
hash: u64,
304-
}
305-
306-
impl Hasher for EntityHasher {
307-
#[inline]
308-
fn finish(&self) -> u64 {
309-
self.hash
310-
}
311-
312-
fn write(&mut self, _bytes: &[u8]) {
313-
panic!("can only hash u64 using EntityHasher");
314-
}
315-
316-
#[inline]
317-
fn write_u64(&mut self, bits: u64) {
318-
// SwissTable (and thus `hashbrown`) cares about two things from the hash:
319-
// - H1: low bits (masked by `2ⁿ-1`) to pick the slot in which to store the item
320-
// - H2: high 7 bits are used to SIMD optimize hash collision probing
321-
// For more see <https://abseil.io/about/design/swisstables#metadata-layout>
322-
323-
// This hash function assumes that the entity ids are still well-distributed,
324-
// so for H1 leaves the entity id alone in the low bits so that id locality
325-
// will also give memory locality for things spawned together.
326-
// For H2, take advantage of the fact that while multiplication doesn't
327-
// spread entropy to the low bits, it's incredibly good at spreading it
328-
// upward, which is exactly where we need it the most.
329-
330-
// While this does include the generation in the output, it doesn't do so
331-
// *usefully*. H1 won't care until you have over 3 billion entities in
332-
// the table, and H2 won't care until something hits generation 33 million.
333-
// Thus the comment suggesting that this is best for live entities,
334-
// where there won't be generation conflicts where it would matter.
335-
336-
// The high 32 bits of this are ⅟φ for Fibonacci hashing. That works
337-
// particularly well for hashing for the same reason as described in
338-
// <https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/>
339-
// It loses no information because it has a modular inverse.
340-
// (Specifically, `0x144c_bc89_u32 * 0x9e37_79b9_u32 == 1`.)
341-
//
342-
// The low 32 bits make that part of the just product a pass-through.
343-
const UPPER_PHI: u64 = 0x9e37_79b9_0000_0001;
344-
345-
// This is `(MAGIC * index + generation) << 32 + index`, in a single instruction.
346-
self.hash = bits.wrapping_mul(UPPER_PHI);
347-
}
348-
}
349-
350-
/// A [`HashMap`] pre-configured to use [`EntityHash`] hashing.
351-
/// Iteration order only depends on the order of insertions and deletions.
352-
pub type EntityHashMap<K, V> = hashbrown::HashMap<K, V, EntityHash>;
353-
354-
/// A [`HashSet`] pre-configured to use [`EntityHash`] hashing.
355-
/// Iteration order only depends on the order of insertions and deletions.
356-
pub type EntityHashSet<T> = hashbrown::HashSet<T, EntityHash>;
357-
358279
/// A specialized hashmap type with Key of [`TypeId`]
359280
/// Iteration order only depends on the order of insertions and deletions.
360281
pub type TypeIdMap<V> = hashbrown::HashMap<TypeId, V, NoOpHash>;

examples/2d/mesh2d_manual.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use bevy::{
99
color::palettes::basic::YELLOW,
1010
core_pipeline::core_2d::{Transparent2d, CORE_2D_DEPTH_FORMAT},
11+
ecs::entity::EntityHashMap,
1112
math::FloatOrd,
1213
prelude::*,
1314
render::{
@@ -33,7 +34,6 @@ use bevy::{
3334
Mesh2dPipelineKey, Mesh2dTransforms, MeshFlags, RenderMesh2dInstance, SetMesh2dBindGroup,
3435
SetMesh2dViewBindGroup, WithMesh2d,
3536
},
36-
utils::EntityHashMap,
3737
};
3838
use std::f32::consts::PI;
3939

@@ -291,7 +291,7 @@ pub const COLORED_MESH2D_SHADER_HANDLE: Handle<Shader> =
291291

292292
/// Our custom pipeline needs its own instance storage
293293
#[derive(Resource, Deref, DerefMut, Default)]
294-
pub struct RenderColoredMesh2dInstances(EntityHashMap<Entity, RenderMesh2dInstance>);
294+
pub struct RenderColoredMesh2dInstances(EntityHashMap<RenderMesh2dInstance>);
295295

296296
impl Plugin for ColoredMesh2dPlugin {
297297
fn build(&self, app: &mut App) {

0 commit comments

Comments
 (0)