From 93e0a4215c915b98cf433ac6d0bcfbc60f0168ec Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Tue, 10 Oct 2017 20:08:11 -0700 Subject: Switch to parking_lot::{Mutex, RwLock} Switch to the `parking_lot` crate's implementations of `std::sync::Mutex` and `std::sync::RwLock`, which are more efficient. A writeup on why `parking_lot` is more efficient can be read here: Upgrade path: Modify `mutex.lock().unwrap()` usage to `mutex.lock()` (not needing to unwrap or handle a result), and `rwlock.read().unwrap()`/`rwlock.write().unwrap()` usage to `rwlock.read()` and `rwlock.write()`. For example, modify: ```rust use serenity::CACHE; println!("{}", CACHE.read().unwrap().user.id); ``` to: ```rust use serenity::CACHE; println!("{}", CACHE.read().user.id); ``` --- src/cache/mod.rs | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) (limited to 'src/cache') diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 9b43804..6b93d41 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -41,10 +41,12 @@ //! [`Role`]: ../model/struct.Role.html //! [`CACHE`]: ../struct.CACHE.html //! [`http`]: ../http/index.html + +use parking_lot::RwLock; use std::collections::hash_map::Entry; use std::collections::{HashMap, HashSet}; use std::default::Default; -use std::sync::{Arc, RwLock}; +use std::sync::Arc; use model::*; mod cache_update; @@ -188,7 +190,7 @@ impl Cache { /// // seconds. /// thread::sleep(Duration::from_secs(5)); /// - /// println!("{} unknown members", CACHE.read().unwrap().unknown_members()); + /// println!("{} unknown members", CACHE.read().unknown_members()); /// } /// } /// @@ -202,7 +204,7 @@ impl Cache { let mut total = 0; for guild in self.guilds.values() { - let guild = guild.read().unwrap(); + let guild = guild.read(); let members = guild.members.len() as u64; @@ -227,7 +229,7 @@ impl Cache { /// ```rust,no_run /// use serenity::client::CACHE; /// - /// let amount = CACHE.read().unwrap().all_private_channels().len(); + /// let amount = CACHE.read().all_private_channels().len(); /// /// println!("There are {} private channels", amount); /// ``` @@ -260,7 +262,7 @@ impl Cache { /// struct Handler; /// impl EventHandler for Handler { /// fn on_ready(&self, _: Context, _: Ready) { - /// println!("Guilds in the Cache: {:?}", CACHE.read().unwrap().all_guilds()); + /// println!("Guilds in the Cache: {:?}", CACHE.read().all_guilds()); /// } /// } /// let mut client = Client::new("token", Handler); @@ -332,10 +334,8 @@ impl Cache { /// # fn try_main() -> Result<(), Box> { /// use serenity::client::CACHE; /// - /// let cache = CACHE.read()?; - /// - /// if let Some(guild) = cache.guild(7) { - /// println!("Guild name: {}", guild.read().unwrap().name); + /// if let Some(guild) = CACHE.read().guild(7) { + /// println!("Guild name: {}", guild.read().name); /// } /// # Ok(()) /// # } @@ -370,7 +370,7 @@ impl Cache { /// /// impl EventHandler for Handler { /// fn on_message(&self, ctx: Context, message: Message) { - /// let cache = CACHE.read().unwrap(); + /// let cache = CACHE.read(); /// /// let channel = match cache.guild_channel(message.channel_id) { /// Some(channel) => channel, @@ -417,10 +417,8 @@ impl Cache { /// # fn try_main() -> Result<(), Box> { /// use serenity::client::CACHE; /// - /// let cache = CACHE.read()?; - /// - /// if let Some(group) = cache.group(7) { - /// println!("Owner Id: {}", group.read().unwrap().owner_id); + /// if let Some(group) = CACHE.read().group(7) { + /// println!("Owner Id: {}", group.read().owner_id); /// } /// # Ok(()) /// # } @@ -448,7 +446,7 @@ impl Cache { /// ```rust,ignore /// use serenity::CACHE; /// - /// let cache = CACHE.read().unwrap(); + /// let cache = CACHE.read(); /// let member = { /// let channel = match cache.guild_channel(message.channel_id) { /// Some(channel) => channel, @@ -482,7 +480,7 @@ impl Cache { pub fn member(&self, guild_id: G, user_id: U) -> Option where G: Into, U: Into { self.guilds.get(&guild_id.into()).and_then(|guild| { - guild.read().unwrap().members.get(&user_id.into()).cloned() + guild.read().members.get(&user_id.into()).cloned() }) } @@ -502,10 +500,8 @@ impl Cache { /// # fn try_main() -> Result<(), Box> { /// use serenity::client::CACHE; /// - /// let cache = CACHE.read()?; - /// - /// if let Some(channel) = cache.private_channel(7) { - /// channel.read().unwrap().say("Hello there!"); + /// if let Some(channel) = CACHE.read().private_channel(7) { + /// channel.read().say("Hello there!"); /// } /// # Ok(()) /// # } @@ -539,9 +535,7 @@ impl Cache { /// # fn try_main() -> Result<(), Box> { /// use serenity::client::CACHE; /// - /// let cache = CACHE.read()?; - /// - /// if let Some(role) = cache.role(7, 77) { + /// if let Some(role) = CACHE.read().role(7, 77) { /// println!("Role with Id 77 is called {}", role.name); /// } /// # Ok(()) @@ -555,7 +549,7 @@ impl Cache { where G: Into, R: Into { self.guilds .get(&guild_id.into()) - .and_then(|g| g.read().unwrap().roles.get(&role_id.into()).cloned()) + .and_then(|g| g.read().roles.get(&role_id.into()).cloned()) } /// Retrieves a `User` from the cache's [`users`] map, if it exists. @@ -576,10 +570,8 @@ impl Cache { /// # fn try_main() -> Result<(), Box> { /// use serenity::client::CACHE; /// - /// let cache = CACHE.read()?; - /// - /// if let Some(user) = cache.user(7) { - /// println!("User with Id 7 is currently named {}", user.read().unwrap().name); + /// if let Some(user) = CACHE.read().user(7) { + /// println!("User with Id 7 is currently named {}", user.read().name); /// } /// # Ok(()) /// # } @@ -611,7 +603,7 @@ impl Cache { e.insert(Arc::new(RwLock::new(user.clone()))); }, Entry::Occupied(mut e) => { - e.get_mut().write().unwrap().clone_from(user); + e.get_mut().write().clone_from(user); }, } } -- cgit v1.2.3