From e939d6c33f75676a358d3605bdd5b97b130b555a Mon Sep 17 00:00:00 2001 From: Christian Hughes <9044780+ItsDoot@users.noreply.github.com> Date: Mon, 9 Sep 2024 10:24:17 -0500 Subject: [PATCH] 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`. --- crates/bevy_ecs/src/observer/mod.rs | 9 ++- crates/bevy_reflect/src/impls/std.rs | 1 - .../src/batching/gpu_preprocessing.rs | 5 +- .../bevy_render/src/view/visibility/range.rs | 10 +-- crates/bevy_utils/src/lib.rs | 79 ------------------- examples/2d/mesh2d_manual.rs | 4 +- 6 files changed, 14 insertions(+), 94 deletions(-) diff --git a/crates/bevy_ecs/src/observer/mod.rs b/crates/bevy_ecs/src/observer/mod.rs index c6ace80ca5202..4711957af9d99 100644 --- a/crates/bevy_ecs/src/observer/mod.rs +++ b/crates/bevy_ecs/src/observer/mod.rs @@ -7,11 +7,12 @@ mod trigger_event; pub use runner::*; pub use trigger_event::*; +use crate::entity::EntityHashMap; use crate::observer::entity_observer::ObservedBy; use crate::{archetype::ArchetypeFlags, system::IntoObserverSystem, world::*}; use crate::{component::ComponentId, prelude::*, world::DeferredWorld}; use bevy_ptr::Ptr; -use bevy_utils::{EntityHashMap, HashMap}; +use bevy_utils::HashMap; use std::{fmt::Debug, marker::PhantomData}; /// Type containing triggered [`Event`] information for a given run of an [`Observer`]. This contains the @@ -152,7 +153,7 @@ pub struct ObserverTrigger { } // Map between an observer entity and its runner -type ObserverMap = EntityHashMap; +type ObserverMap = EntityHashMap; /// Collection of [`ObserverRunner`] for [`Observer`] registered to a particular trigger targeted at a specific component. #[derive(Default, Debug)] @@ -160,7 +161,7 @@ pub struct CachedComponentObservers { // Observers listening to triggers targeting this component map: ObserverMap, // Observers listening to triggers targeting this component on a specific entity - entity_map: EntityHashMap, + entity_map: EntityHashMap, } /// Collection of [`ObserverRunner`] for [`Observer`] registered to a particular trigger. @@ -171,7 +172,7 @@ pub struct CachedObservers { // Observers listening for this trigger fired at a specific component component_observers: HashMap, // Observers listening for this trigger fired at a specific entity - entity_observers: EntityHashMap, + entity_observers: EntityHashMap, } /// Metadata for observers. Stores a cache mapping trigger ids to the registered observers. diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index c9f27a4cb34a4..85f63be654340 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -1029,7 +1029,6 @@ macro_rules! impl_reflect_for_hashset { } impl_type_path!(::bevy_utils::NoOpHash); -impl_type_path!(::bevy_utils::EntityHash); impl_type_path!(::bevy_utils::FixedState); impl_reflect_for_hashset!(::std::collections::HashSet); diff --git a/crates/bevy_render/src/batching/gpu_preprocessing.rs b/crates/bevy_render/src/batching/gpu_preprocessing.rs index 7d5ce067f5fbc..bd515c0840625 100644 --- a/crates/bevy_render/src/batching/gpu_preprocessing.rs +++ b/crates/bevy_render/src/batching/gpu_preprocessing.rs @@ -3,14 +3,13 @@ use bevy_app::{App, Plugin}; use bevy_derive::{Deref, DerefMut}; use bevy_ecs::{ - entity::Entity, + entity::{Entity, EntityHashMap}, query::{Has, With}, schedule::IntoSystemConfigs as _, system::{Query, Res, ResMut, Resource, StaticSystemParam}, world::{FromWorld, World}, }; use bevy_encase_derive::ShaderType; -use bevy_utils::EntityHashMap; use bytemuck::{Pod, Zeroable}; use nonmax::NonMaxU32; use smallvec::smallvec; @@ -99,7 +98,7 @@ where /// corresponds to each instance. /// /// This is keyed off each view. Each view has a separate buffer. - pub work_item_buffers: EntityHashMap, + pub work_item_buffers: EntityHashMap, /// The uniform data inputs for the current frame. /// diff --git a/crates/bevy_render/src/view/visibility/range.rs b/crates/bevy_render/src/view/visibility/range.rs index d1e1d6546fa14..d9792d0296cc7 100644 --- a/crates/bevy_render/src/view/visibility/range.rs +++ b/crates/bevy_render/src/view/visibility/range.rs @@ -9,7 +9,7 @@ use std::{ use bevy_app::{App, Plugin, PostUpdate}; use bevy_ecs::{ component::Component, - entity::Entity, + entity::{Entity, EntityHashMap}, query::{Changed, With}, schedule::IntoSystemConfigs as _, system::{Query, Res, ResMut, Resource}, @@ -17,7 +17,7 @@ use bevy_ecs::{ use bevy_math::{vec4, FloatOrd, Vec4}; use bevy_reflect::Reflect; use bevy_transform::components::GlobalTransform; -use bevy_utils::{prelude::default, EntityHashMap, HashMap}; +use bevy_utils::{prelude::default, HashMap}; use nonmax::NonMaxU16; use wgpu::{BufferBindingType, BufferUsages}; @@ -191,7 +191,7 @@ impl VisibilityRange { #[derive(Resource)] pub struct RenderVisibilityRanges { /// Information corresponding to each entity. - entities: EntityHashMap, + entities: EntityHashMap, /// Maps a [`VisibilityRange`] to its index within the `buffer`. /// @@ -309,13 +309,13 @@ impl RenderVisibilityRanges { #[derive(Resource, Default)] pub struct VisibleEntityRanges { /// Stores which bit index each view corresponds to. - views: EntityHashMap, + views: EntityHashMap, /// Stores a bitmask in which each view has a single bit. /// /// A 0 bit for a view corresponds to "out of range"; a 1 bit corresponds to /// "in range". - entities: EntityHashMap, + entities: EntityHashMap, } impl VisibleEntityRanges { diff --git a/crates/bevy_utils/src/lib.rs b/crates/bevy_utils/src/lib.rs index b53bcd3e2ea89..4c04b793df424 100644 --- a/crates/bevy_utils/src/lib.rs +++ b/crates/bevy_utils/src/lib.rs @@ -276,85 +276,6 @@ impl PreHashMapExt for PreHashMap Self::Hasher { - EntityHasher::default() - } -} - -/// A very fast hash that is only designed to work on generational indices -/// like `Entity`. It will panic if attempting to hash a type containing -/// non-u64 fields. -/// -/// This is heavily optimized for typical cases, where you have mostly live -/// entities, and works particularly well for contiguous indices. -/// -/// If you have an unusual case -- say all your indices are multiples of 256 -/// or most of the entities are dead generations -- then you might want also to -/// try [`AHasher`] for a slower hash computation but fewer lookup conflicts. -#[derive(Debug, Default)] -pub struct EntityHasher { - hash: u64, -} - -impl Hasher for EntityHasher { - #[inline] - fn finish(&self) -> u64 { - self.hash - } - - fn write(&mut self, _bytes: &[u8]) { - panic!("can only hash u64 using EntityHasher"); - } - - #[inline] - fn write_u64(&mut self, bits: u64) { - // SwissTable (and thus `hashbrown`) cares about two things from the hash: - // - H1: low bits (masked by `2ⁿ-1`) to pick the slot in which to store the item - // - H2: high 7 bits are used to SIMD optimize hash collision probing - // For more see - - // This hash function assumes that the entity ids are still well-distributed, - // so for H1 leaves the entity id alone in the low bits so that id locality - // will also give memory locality for things spawned together. - // For H2, take advantage of the fact that while multiplication doesn't - // spread entropy to the low bits, it's incredibly good at spreading it - // upward, which is exactly where we need it the most. - - // While this does include the generation in the output, it doesn't do so - // *usefully*. H1 won't care until you have over 3 billion entities in - // the table, and H2 won't care until something hits generation 33 million. - // Thus the comment suggesting that this is best for live entities, - // where there won't be generation conflicts where it would matter. - - // The high 32 bits of this are ⅟φ for Fibonacci hashing. That works - // particularly well for hashing for the same reason as described in - // - // It loses no information because it has a modular inverse. - // (Specifically, `0x144c_bc89_u32 * 0x9e37_79b9_u32 == 1`.) - // - // The low 32 bits make that part of the just product a pass-through. - const UPPER_PHI: u64 = 0x9e37_79b9_0000_0001; - - // This is `(MAGIC * index + generation) << 32 + index`, in a single instruction. - self.hash = bits.wrapping_mul(UPPER_PHI); - } -} - -/// A [`HashMap`] pre-configured to use [`EntityHash`] hashing. -/// Iteration order only depends on the order of insertions and deletions. -pub type EntityHashMap = hashbrown::HashMap; - -/// A [`HashSet`] pre-configured to use [`EntityHash`] hashing. -/// Iteration order only depends on the order of insertions and deletions. -pub type EntityHashSet = hashbrown::HashSet; - /// A specialized hashmap type with Key of [`TypeId`] /// Iteration order only depends on the order of insertions and deletions. pub type TypeIdMap = hashbrown::HashMap; diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index b298a12fec24c..4361c17b132cc 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -8,6 +8,7 @@ use bevy::{ color::palettes::basic::YELLOW, core_pipeline::core_2d::{Transparent2d, CORE_2D_DEPTH_FORMAT}, + ecs::entity::EntityHashMap, math::FloatOrd, prelude::*, render::{ @@ -33,7 +34,6 @@ use bevy::{ Mesh2dPipelineKey, Mesh2dTransforms, MeshFlags, RenderMesh2dInstance, SetMesh2dBindGroup, SetMesh2dViewBindGroup, WithMesh2d, }, - utils::EntityHashMap, }; use std::f32::consts::PI; @@ -291,7 +291,7 @@ pub const COLORED_MESH2D_SHADER_HANDLE: Handle = /// Our custom pipeline needs its own instance storage #[derive(Resource, Deref, DerefMut, Default)] -pub struct RenderColoredMesh2dInstances(EntityHashMap); +pub struct RenderColoredMesh2dInstances(EntityHashMap); impl Plugin for ColoredMesh2dPlugin { fn build(&self, app: &mut App) {