diff options
| author | Zeyla Hellyer <[email protected]> | 2017-02-09 13:34:08 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-02-09 13:34:08 -0800 |
| commit | 0c9ec377aa7281fb3d4bc390c896b426660a5387 (patch) | |
| tree | a355bda0c0d02d8b67331e0a99090c7b26206cb1 /src/ext/framework | |
| parent | Release v0.1.5 (diff) | |
| download | serenity-0c9ec377aa7281fb3d4bc390c896b426660a5387.tar.xz serenity-0c9ec377aa7281fb3d4bc390c896b426660a5387.zip | |
Optimize caching
Improve the cache by keeping track of new maps, making other maps have
`Arc<RwLock>` values, optimizing already-existing methods, and take advantage
of new, more efficient retrievals (e.g. simply keying a value from a map rather
than iterating over vecs or maps and then itering over another vec).
Keep track of two new maps in the cache:
- **channels**: a map of all guild channels that exist, so that they can be
efficiently found, and so a message's guild can be efficiently found
- **users**: a map of all users that exist, so that it can be shared across
all members and presences
Other cache fields now have `Arc<RwLock>` values:
- `groups`
- `guilds`
- `private_channels`
`Cache::unavailable_guilds` is now a `HashSet<GuildId>` instead of a
`Vec<GuildId>`. This should slightly optimize removals/insertions for large
bots.
`ext::cache::ChannelRef` has been removed as it became equivilant in
functionality to `model::Channel`. Also, `model::Channel` now has all variant
data encased in `Arc<RwLock>`s. E.g., `Channel::Group(Group)` is now
`Channel::Group(Arc<RwLock<Group>>)`.
Some model struct fields are now wrapped in an `Arc<RwLock>`. These are:
- `Group::recipients`: `HashMap<UserId, User>` -> `HashMap<UserId, Arc<RwLock<User>>>`
- `Guild::channels`: `HashMap<ChannelId, GuildChannel>` -> `HashMap<ChannelId, Arc<RwLock<GuildChannel>>>`
- `Member::user`: `User` -> `Arc<RwLock<User>>`
- `PrivateChannel::recipient`: `User` -> `Arc<RwLock<User>>`
Some (cache-enabled) event handler signatures have changed to use
`Arc<RwLock>`s:
- `Client::on_call_delete`
- `Client::on_call_update`
- `Client::on_guild_delete`
- `Client::on_guild_update`
Many function signatures have changed:
- `Cache::get_call` now returns a `Option<Arc<RwLock<Call>>>` instead of a
`Option<&Call>`
- `Cache::get_channel` now returns a `Option<Channel>` instead of a
`Option<ChannelRef>`. This now also retrieves directly from the
`Guild::channels` instead of iterating over guilds' for a guild channel
- `Cache::get_guild` now returns a `Option<Arc<RwLock<Guild>>>` instead of a
`Option<&Guild>`
- `Cache::get_guild_channel` now returns a `Option<Arc<RwLock<GuildChannel>>>`
instead of a `Option<&GuildChannel>`
- `Cache::get_group` now returns a `Option<Arc<RwLock<Group>>>` instead of a
`Option<&Group>`
- `Cache::get_member` now returns a `Option<Member>` instead of a
`Option<&Member>`, due to guilds being behind a lock themselves
- `Cache::get_role` now returns a `Option<Role>` instead of a `Option<&Role>`
for the above reason
- `Cache::get_user` now returns a `Option<Arc<RwLock<User>>>` instead of a
`Option<&User>`
- `GuildId::find` now returns a `Option<Arc<RwLock<Guild>>>` instead of a
`Option<Guild>`
- `UserId::find` now returns a `Option<Arc<RwLock<User>>>` instead of a
`Option<User>`
- `Member::display_name` now returns a `Cow<String>` instead of a `&str`
A new cache method has been added, `Cache::get_private_channel`, to retrieve a
`PrivateChannel`.
The `Display` formatter for `Channel` has been optimized to not clone.
Diffstat (limited to 'src/ext/framework')
| -rw-r--r-- | src/ext/framework/mod.rs | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs index 76f3418..30a5c8f 100644 --- a/src/ext/framework/mod.rs +++ b/src/ext/framework/mod.rs @@ -74,13 +74,11 @@ use std::default::Default; use std::sync::Arc; use std::thread; use ::client::Context; -use ::model::{Message, UserId}; +use ::model::{Channel, Message, UserId}; use ::utils; #[cfg(feature="cache")] use ::client::CACHE; -#[cfg(feature="cache")] -use ::ext::cache::ChannelRef; /// A macro to generate "named parameters". This is useful to avoid manually /// using the "arguments" parameter and manually parsing types. @@ -410,7 +408,7 @@ impl Framework { let guild_id = { match CACHE.read().unwrap().get_channel(message.channel_id) { - Some(ChannelRef::Guild(channel)) => Some(channel.guild_id), + Some(Channel::Guild(channel)) => Some(channel.read().unwrap().guild_id), _ => None, } }; @@ -425,7 +423,7 @@ impl Framework { } if let Some(guild) = guild_id.find() { - if self.configuration.blocked_users.contains(&guild.owner_id) { + if self.configuration.blocked_users.contains(&guild.read().unwrap().owner_id) { if let Some(ref message) = self.configuration.blocked_guild_message { let _ = context.say(message); } @@ -515,9 +513,11 @@ impl Framework { let member = { let mut member_found = None; - if let Some(ChannelRef::Guild(channel)) = cache.get_channel(message.channel_id) { - if let Some(guild) = channel.guild_id.find() { - if let Some(member) = guild.members.get(&message.author.id) { + if let Some(Channel::Guild(channel)) = cache.get_channel(message.channel_id) { + let guild_id = channel.read().unwrap().guild_id; + + if let Some(guild) = guild_id.find() { + if let Some(member) = guild.read().unwrap().members.get(&message.author.id) { member_found = Some(member.clone()); } } @@ -529,7 +529,7 @@ impl Framework { if let Some(member) = member { if let Ok(guild_id) = member.find_guild() { if let Some(guild) = cache.get_guild(guild_id) { - let perms = guild.permissions_for(message.channel_id, message.author.id); + let perms = guild.read().unwrap().permissions_for(message.channel_id, message.author.id); permissions_fulfilled = perms.contains(command.required_permissions); } |