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/client/mod.rs | |
| 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/client/mod.rs')
| -rw-r--r-- | src/client/mod.rs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/client/mod.rs b/src/client/mod.rs index 5ef770a..d0593fe 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -868,7 +868,7 @@ impl Client { /// /// [`CallDelete`]: ../model/event/enum.Event.html#variant.CallDelete pub fn on_call_delete<F>(&mut self, handler: F) - where F: Fn(Context, ChannelId, Option<Call>) + Send + Sync + 'static { + where F: Fn(Context, ChannelId, Option<Arc<RwLock<Call>>>) + Send + Sync + 'static { self.event_store.write() .unwrap() .on_call_delete = Some(Arc::new(handler)); @@ -878,7 +878,7 @@ impl Client { /// /// [`CallUpdate`]: ../model/event/enum.Event.html#variant.CallUpdate pub fn on_call_update<F>(&mut self, handler: F) - where F: Fn(Context, Option<Call>, Option<Call>) + Send + Sync + 'static { + where F: Fn(Context, Option<Arc<RwLock<Call>>>, Option<Arc<RwLock<Call>>>) + Send + Sync + 'static { self.event_store.write() .unwrap() .on_call_update = Some(Arc::new(handler)); @@ -910,7 +910,7 @@ impl Client { /// [`Role`]: ../model/struct.Role.html /// [`Cache`]: ../ext/cache/struct.Cache.html pub fn on_guild_delete<F>(&mut self, handler: F) - where F: Fn(Context, PartialGuild, Option<Guild>) + Send + Sync + 'static { + where F: Fn(Context, PartialGuild, Option<Arc<RwLock<Guild>>>) + Send + Sync + 'static { self.event_store.write() .unwrap() .on_guild_delete = Some(Arc::new(handler)); @@ -977,7 +977,7 @@ impl Client { /// /// [`GuildUpdate`]: ../model/event/enum.Event.html#variant.GuildUpdate pub fn on_guild_update<F>(&mut self, handler: F) - where F: Fn(Context, Option<Guild>, PartialGuild) + Send + Sync + 'static { + where F: Fn(Context, Option<Arc<RwLock<Guild>>>, PartialGuild) + Send + Sync + 'static { self.event_store.write() .unwrap() .on_guild_update = Some(Arc::new(handler)); |