From 965f1e683e5b2a3b6c933f674efa17d9d304a4df Mon Sep 17 00:00:00 2001 From: shuo Date: Sun, 12 Feb 2023 21:30:57 +0800 Subject: [PATCH 1/5] use bevy_utils::HashMap for better performance. TypeId is predefined u64, so hash safety is not a concern --- crates/bevy_ecs/src/bundle.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index 042138a3c0db8..ab857b58bb3b8 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -15,7 +15,8 @@ use crate::{ }; use bevy_ecs_macros::all_tuples; use bevy_ptr::OwningPtr; -use std::{any::TypeId, collections::HashMap}; +use bevy_utils::HashMap; +use std::any::TypeId; /// The `Bundle` trait enables insertion and removal of [`Component`]s from an entity. /// From 671e843eb09b378d4cde8fdd5897dea461701b67 Mon Sep 17 00:00:00 2001 From: shuo Date: Sun, 12 Feb 2023 23:28:01 +0800 Subject: [PATCH 2/5] use bevy_utils::StableHashMap for HashMap with TypeId as key, use bevy_utils::HashMap for archetype --- crates/bevy_ecs/Cargo.toml | 1 - crates/bevy_ecs/src/archetype.rs | 3 +-- crates/bevy_ecs/src/bundle.rs | 4 ++-- crates/bevy_ecs/src/component.rs | 5 +++-- crates/bevy_ecs/src/lib.rs | 5 +++++ 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/bevy_ecs/Cargo.toml b/crates/bevy_ecs/Cargo.toml index 15c8dd2bb3e51..64520ca1d1beb 100644 --- a/crates/bevy_ecs/Cargo.toml +++ b/crates/bevy_ecs/Cargo.toml @@ -24,7 +24,6 @@ async-channel = "1.4" event-listener = "2.5" thread_local = "1.1.4" fixedbitset = "0.4.2" -fxhash = "0.2" downcast-rs = "1.2" serde = { version = "1", features = ["derive"] } diff --git a/crates/bevy_ecs/src/archetype.rs b/crates/bevy_ecs/src/archetype.rs index 777dd0d2e3fcd..bd2ceb5dcc582 100644 --- a/crates/bevy_ecs/src/archetype.rs +++ b/crates/bevy_ecs/src/archetype.rs @@ -26,7 +26,6 @@ use crate::{ storage::{ImmutableSparseSet, SparseArray, SparseSet, SparseSetIndex, TableId, TableRow}, }; use std::{ - collections::HashMap, hash::Hash, ops::{Index, IndexMut}, }; @@ -601,7 +600,7 @@ impl SparseSetIndex for ArchetypeComponentId { pub struct Archetypes { pub(crate) archetypes: Vec, pub(crate) archetype_component_count: usize, - archetype_ids: HashMap, + archetype_ids: bevy_utils::HashMap, } impl Archetypes { diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index ab857b58bb3b8..34d9b428ca2c1 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -12,10 +12,10 @@ use crate::{ component::{Component, ComponentId, ComponentStorage, Components, StorageType, Tick}, entity::{Entities, Entity, EntityLocation}, storage::{SparseSetIndex, SparseSets, Storages, Table, TableRow}, + TypeIdMap, }; use bevy_ecs_macros::all_tuples; use bevy_ptr::OwningPtr; -use bevy_utils::HashMap; use std::any::TypeId; /// The `Bundle` trait enables insertion and removal of [`Component`]s from an entity. @@ -684,7 +684,7 @@ impl<'a, 'b> BundleSpawner<'a, 'b> { #[derive(Default)] pub struct Bundles { bundle_infos: Vec, - bundle_ids: HashMap, + bundle_ids: TypeIdMap, } impl Bundles { diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index 745c75df6ff89..8d311686942b5 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -5,6 +5,7 @@ use crate::{ storage::{SparseSetIndex, Storages}, system::{Local, Resource}, world::{FromWorld, World}, + TypeIdMap, }; pub use bevy_ecs_macros::Component; use bevy_ptr::{OwningPtr, UnsafeCellDeref}; @@ -400,8 +401,8 @@ impl ComponentDescriptor { #[derive(Debug, Default)] pub struct Components { components: Vec, - indices: std::collections::HashMap, - resource_indices: std::collections::HashMap, + indices: TypeIdMap, + resource_indices: TypeIdMap, } impl Components { diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 77d6befc5d68d..472f1ffe9190d 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -19,6 +19,8 @@ pub mod storage; pub mod system; pub mod world; +use std::any::TypeId; + pub use bevy_ptr as ptr; /// Most commonly used re-exported types. @@ -52,6 +54,9 @@ pub mod prelude { pub use bevy_ecs_macros::all_tuples; +/// HashMap type with `TypeId` as Key +type TypeIdMap = bevy_utils::StableHashMap; + #[cfg(test)] mod tests { use crate as bevy_ecs; From 6ec25a8b71691fc5a29c0d1a6b6fd03a6dc7e92a Mon Sep 17 00:00:00 2001 From: shuo Date: Sun, 12 Feb 2023 23:34:48 +0800 Subject: [PATCH 3/5] ci clippy --- crates/bevy_ecs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 472f1ffe9190d..088a64c4fb73f 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -54,7 +54,7 @@ pub mod prelude { pub use bevy_ecs_macros::all_tuples; -/// HashMap type with `TypeId` as Key +/// A specialized hashmap type with Key of `TypeId` type TypeIdMap = bevy_utils::StableHashMap; #[cfg(test)] From 72e18d9b82c237955895fa2d047244a05f6aa5bd Mon Sep 17 00:00:00 2001 From: shuo Date: Mon, 13 Feb 2023 20:50:33 +0800 Subject: [PATCH 4/5] use FxHash for TypeIdMap --- crates/bevy_ecs/Cargo.toml | 1 + crates/bevy_ecs/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/Cargo.toml b/crates/bevy_ecs/Cargo.toml index 64520ca1d1beb..15c8dd2bb3e51 100644 --- a/crates/bevy_ecs/Cargo.toml +++ b/crates/bevy_ecs/Cargo.toml @@ -24,6 +24,7 @@ async-channel = "1.4" event-listener = "2.5" thread_local = "1.1.4" fixedbitset = "0.4.2" +fxhash = "0.2" downcast-rs = "1.2" serde = { version = "1", features = ["derive"] } diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 088a64c4fb73f..5fe9d43a0cc00 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -55,7 +55,7 @@ pub mod prelude { pub use bevy_ecs_macros::all_tuples; /// A specialized hashmap type with Key of `TypeId` -type TypeIdMap = bevy_utils::StableHashMap; +type TypeIdMap = std::collections::HashMap; #[cfg(test)] mod tests { From 927353f2b7a32f1428059a0648ff7af9f883e982 Mon Sep 17 00:00:00 2001 From: shuo Date: Tue, 14 Feb 2023 14:35:23 +0800 Subject: [PATCH 5/5] replace fxhash => rustc-hash --- crates/bevy_ecs/Cargo.toml | 2 +- crates/bevy_ecs/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/Cargo.toml b/crates/bevy_ecs/Cargo.toml index 15c8dd2bb3e51..4183b615a999c 100644 --- a/crates/bevy_ecs/Cargo.toml +++ b/crates/bevy_ecs/Cargo.toml @@ -24,7 +24,7 @@ async-channel = "1.4" event-listener = "2.5" thread_local = "1.1.4" fixedbitset = "0.4.2" -fxhash = "0.2" +rustc-hash = "1.1" downcast-rs = "1.2" serde = { version = "1", features = ["derive"] } diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 5fe9d43a0cc00..38125d0b56dd5 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -55,7 +55,7 @@ pub mod prelude { pub use bevy_ecs_macros::all_tuples; /// A specialized hashmap type with Key of `TypeId` -type TypeIdMap = std::collections::HashMap; +type TypeIdMap = rustc_hash::FxHashMap; #[cfg(test)] mod tests {