From 3b30b241f4e8939e8652d0e12b1de989b75f4269 Mon Sep 17 00:00:00 2001 From: Felipe Jorge Date: Sun, 20 Dec 2020 19:40:52 -0300 Subject: [PATCH 1/3] alternative name component --- crates/bevy_core/Cargo.toml | 3 ++ crates/bevy_core/src/lib.rs | 3 ++ crates/bevy_core/src/name.rs | 91 ++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 crates/bevy_core/src/name.rs diff --git a/crates/bevy_core/Cargo.toml b/crates/bevy_core/Cargo.toml index 7986e54f4ba55..5cb37952c5cce 100644 --- a/crates/bevy_core/Cargo.toml +++ b/crates/bevy_core/Cargo.toml @@ -21,3 +21,6 @@ bevy_math = { path = "../bevy_math", version = "0.4.0" } bevy_reflect = { path = "../bevy_reflect", version = "0.4.0", features = ["bevy"] } bevy_tasks = { path = "../bevy_tasks", version = "0.4.0" } bevy_utils = { path = "../bevy_utils", version = "0.4.0" } + +# other +ahash = "0.6.2" \ No newline at end of file diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 468879e2363dc..c99dedb61f738 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -1,6 +1,7 @@ mod bytes; mod float_ord; mod label; +mod name; mod task_pool_options; mod time; @@ -11,6 +12,7 @@ use bevy_reflect::RegisterTypeBuilder; pub use bytes::*; pub use float_ord::*; pub use label::*; +pub use name::*; pub use task_pool_options::DefaultTaskPoolOptions; pub use time::*; @@ -36,6 +38,7 @@ impl Plugin for CorePlugin { .init_resource::() .init_resource::() .register_type::>() + .register_type::() .register_type::>() .register_type::() .add_system_to_stage(stage::FIRST, time_system.system()) diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs new file mode 100644 index 0000000000000..019d20efaa69a --- /dev/null +++ b/crates/bevy_core/src/name.rs @@ -0,0 +1,91 @@ +use bevy_reflect::{Reflect, ReflectComponent}; +use std::hash::{Hash, Hasher}; +use std::ops::Deref; + +/// Component used to identify a entity. Stores a hash for faster comparisons +#[derive(Debug, Clone, Reflect)] +#[reflect(Component)] +pub struct Name { + hash: u64, // TODO: Shouldn't be serialized + name: String, +} + +impl Default for Name { + fn default() -> Self { + Name::new("".to_string()) + } +} + +impl Name { + pub fn new(name: String) -> Self { + let mut name = Name { name, hash: 0 }; + name.update_hash(); + name + } + + #[inline(always)] + pub fn from_str(name: &str) -> Self { + Name::new(name.to_owned()) + } + + #[inline(always)] + pub fn set(&mut self, name: String) { + *self = Name::new(name); + } + + #[inline(always)] + pub fn mutate(&mut self, f: F) { + f(&mut self.name); + self.update_hash(); + } + + #[inline(always)] + pub fn as_str(&self) -> &str { + self.name.as_str() + } + + fn update_hash(&mut self) { + let mut hasher = ahash::AHasher::default(); + self.name.hash(&mut hasher); + self.hash = hasher.finish(); + } +} + +impl Hash for Name { + fn hash(&self, state: &mut H) { + self.name.hash(state); + } +} + +impl PartialEq for Name { + fn eq(&self, other: &Self) -> bool { + if self.hash != other.hash { + // Makes the common case of two strings not been equal very fast + return false; + } + + self.name.eq(&other.name) + } +} + +impl Eq for Name {} + +impl PartialOrd for Name { + fn partial_cmp(&self, other: &Self) -> Option { + self.name.partial_cmp(&other.name) + } +} + +impl Ord for Name { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.name.cmp(&other.name) + } +} + +impl Deref for Name { + type Target = String; + + fn deref(&self) -> &Self::Target { + &self.name + } +} From 3b64b287e50596fbd9676da8e703ddc004df4170 Mon Sep 17 00:00:00 2001 From: lassade Date: Tue, 22 Dec 2020 00:31:10 -0300 Subject: [PATCH 2/3] fixes ahash import and format --- crates/bevy_core/Cargo.toml | 5 +---- crates/bevy_core/src/name.rs | 9 ++++++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/bevy_core/Cargo.toml b/crates/bevy_core/Cargo.toml index 5cb37952c5cce..b4ad35e275005 100644 --- a/crates/bevy_core/Cargo.toml +++ b/crates/bevy_core/Cargo.toml @@ -20,7 +20,4 @@ bevy_ecs = { path = "../bevy_ecs", version = "0.4.0" } bevy_math = { path = "../bevy_math", version = "0.4.0" } bevy_reflect = { path = "../bevy_reflect", version = "0.4.0", features = ["bevy"] } bevy_tasks = { path = "../bevy_tasks", version = "0.4.0" } -bevy_utils = { path = "../bevy_utils", version = "0.4.0" } - -# other -ahash = "0.6.2" \ No newline at end of file +bevy_utils = { path = "../bevy_utils", version = "0.4.0" } \ No newline at end of file diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs index 019d20efaa69a..9eb24124b7517 100644 --- a/crates/bevy_core/src/name.rs +++ b/crates/bevy_core/src/name.rs @@ -1,6 +1,9 @@ use bevy_reflect::{Reflect, ReflectComponent}; -use std::hash::{Hash, Hasher}; -use std::ops::Deref; +use bevy_utils::AHasher; +use std::{ + hash::{Hash, Hasher}, + ops::Deref, +}; /// Component used to identify a entity. Stores a hash for faster comparisons #[derive(Debug, Clone, Reflect)] @@ -45,7 +48,7 @@ impl Name { } fn update_hash(&mut self) { - let mut hasher = ahash::AHasher::default(); + let mut hasher = AHasher::default(); self.name.hash(&mut hasher); self.hash = hasher.finish(); } From 88c31c06dedc7a28efb56c59bd9f1bd8da5c6b6c Mon Sep 17 00:00:00 2001 From: lassade Date: Tue, 22 Dec 2020 00:57:58 -0300 Subject: [PATCH 3/3] fix clippy errors --- crates/bevy_core/src/name.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/bevy_core/src/name.rs b/crates/bevy_core/src/name.rs index 9eb24124b7517..35c7cea399d1c 100644 --- a/crates/bevy_core/src/name.rs +++ b/crates/bevy_core/src/name.rs @@ -26,11 +26,6 @@ impl Name { name } - #[inline(always)] - pub fn from_str(name: &str) -> Self { - Name::new(name.to_owned()) - } - #[inline(always)] pub fn set(&mut self, name: String) { *self = Name::new(name); @@ -54,6 +49,13 @@ impl Name { } } +impl From<&str> for Name { + #[inline(always)] + fn from(name: &str) -> Self { + Name::new(name.to_owned()) + } +} + impl Hash for Name { fn hash(&self, state: &mut H) { self.name.hash(state);