Skip to content

Commit

Permalink
Gateway: Move from RwLock<HashMap> to DashMap
Browse files Browse the repository at this point in the history
Moves to the faster dashmap in the Songbird management struct, as the final v4 brought back the `entry` API that I was needing to use it safely.

Also handles some new clippy lints.
  • Loading branch information
FelixMcFelix committed Jan 26, 2021
1 parent 658fd83 commit a0e905a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ version = "0.2"
optional = true
version = "1"

[dependencies.dashmap]
optional = true
version = "4"

[dependencies.discortp]
features = ["discord-full"]
optional = true
Expand Down Expand Up @@ -117,6 +121,7 @@ default = [
"gateway",
]
gateway = [
"dashmap",
"flume",
"parking_lot",
"tokio/sync",
Expand Down
4 changes: 2 additions & 2 deletions src/driver/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ impl CryptoMode {
let (tag_bytes, data_bytes) = body_remaining.split_at_mut(body_start);
let tag = Tag::from_slice(tag_bytes);

Ok(cipher
cipher
.decrypt_in_place_detached(nonce_slice, b"", data_bytes, tag)
.map(|_| (body_start, body_tail))?)
.map(|_| (body_start, body_tail))
}

/// Encrypts a Discord RT(C)P packet using the given key.
Expand Down
17 changes: 8 additions & 9 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
};
#[cfg(feature = "serenity")]
use async_trait::async_trait;
use dashmap::DashMap;
#[cfg(feature = "serenity")]
use futures::channel::mpsc::UnboundedSender as Sender;
use parking_lot::RwLock as PRwLock;
Expand All @@ -21,7 +22,7 @@ use serenity::{
voice::VoiceState,
},
};
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;
use tokio::sync::Mutex;
#[cfg(feature = "twilight")]
use twilight_gateway::Cluster;
Expand All @@ -44,7 +45,7 @@ struct ClientData {
#[derive(Debug)]
pub struct Songbird {
client_data: PRwLock<ClientData>,
calls: PRwLock<HashMap<GuildId, Arc<Mutex<Call>>>>,
calls: DashMap<GuildId, Arc<Mutex<Call>>>,
sharder: Sharder,

#[cfg(feature = "driver")]
Expand Down Expand Up @@ -117,8 +118,9 @@ impl Songbird {
///
/// [`Call`]: Call
pub fn get<G: Into<GuildId>>(&self, guild_id: G) -> Option<Arc<Mutex<Call>>> {
let map_read = self.calls.read();
map_read.get(&guild_id.into()).cloned()
self.calls
.get(&guild_id.into())
.map(|mapref| Arc::clone(&mapref))
}

/// Retrieves a [`Call`] for the given guild, creating a new one if
Expand All @@ -129,9 +131,7 @@ impl Songbird {
/// [`Call`]: Call
pub fn get_or_insert(&self, guild_id: GuildId) -> Arc<Mutex<Call>> {
self.get(guild_id).unwrap_or_else(|| {
let mut map_read = self.calls.write();

map_read
self.calls
.entry(guild_id)
.or_insert_with(|| {
let info = self.manager_info();
Expand Down Expand Up @@ -301,8 +301,7 @@ impl Songbird {

async fn _remove(&self, guild_id: GuildId) -> JoinResult<()> {
self.leave(guild_id).await?;
let mut calls = self.calls.write();
calls.remove(&guild_id);
self.calls.remove(&guild_id);
Ok(())
}
}
Expand Down

0 comments on commit a0e905a

Please sign in to comment.