Minor optimization in StringKey, making it from an Arc<String> into an Arc<str>
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-06-12 18:18:26 +02:00
parent 55380f631f
commit f0bc62ce19
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
1 changed files with 4 additions and 4 deletions

View File

@ -8,12 +8,12 @@ use std::sync::{Arc, Mutex, Weak};
/// `charmander` == `Charmander`. /// `charmander` == `Charmander`.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct StringKey { pub struct StringKey {
str: Arc<String>, str: Arc<str>,
hash: u32, hash: u32,
} }
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref STRING_CACHE: Mutex<HashMap<u32, Weak<String>>> = Mutex::new(HashMap::new()); static ref STRING_CACHE: Mutex<HashMap<u32, Weak<str>>> = Mutex::new(HashMap::new());
} }
impl StringKey { impl StringKey {
@ -49,7 +49,7 @@ impl StringKey {
} }
} }
let v = Self { let v = Self {
str: Arc::new(s.to_string()), str: s.into(),
hash, hash,
}; };
cache.insert(hash, Arc::downgrade(&v.str)); cache.insert(hash, Arc::downgrade(&v.str));
@ -57,7 +57,7 @@ impl StringKey {
} }
pub fn str(&self) -> &str { pub fn str(&self) -> &str {
self.str.as_str() &self.str
} }
pub fn hash(&self) -> u32 { pub fn hash(&self) -> u32 {