Gateway: Move from RwLock<HashMap> to DashMap

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.
This commit is contained in:
Kyle Simpson
2021-01-26 14:23:07 +00:00
parent 658fd830c1
commit a0e905a83f
3 changed files with 15 additions and 11 deletions

View File

@@ -36,6 +36,10 @@ version = "0.2"
optional = true optional = true
version = "1" version = "1"
[dependencies.dashmap]
optional = true
version = "4"
[dependencies.discortp] [dependencies.discortp]
features = ["discord-full"] features = ["discord-full"]
optional = true optional = true
@@ -117,6 +121,7 @@ default = [
"gateway", "gateway",
] ]
gateway = [ gateway = [
"dashmap",
"flume", "flume",
"parking_lot", "parking_lot",
"tokio/sync", "tokio/sync",

View File

@@ -131,9 +131,9 @@ impl CryptoMode {
let (tag_bytes, data_bytes) = body_remaining.split_at_mut(body_start); let (tag_bytes, data_bytes) = body_remaining.split_at_mut(body_start);
let tag = Tag::from_slice(tag_bytes); let tag = Tag::from_slice(tag_bytes);
Ok(cipher cipher
.decrypt_in_place_detached(nonce_slice, b"", data_bytes, tag) .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. /// Encrypts a Discord RT(C)P packet using the given key.

View File

@@ -9,6 +9,7 @@ use crate::{
}; };
#[cfg(feature = "serenity")] #[cfg(feature = "serenity")]
use async_trait::async_trait; use async_trait::async_trait;
use dashmap::DashMap;
#[cfg(feature = "serenity")] #[cfg(feature = "serenity")]
use futures::channel::mpsc::UnboundedSender as Sender; use futures::channel::mpsc::UnboundedSender as Sender;
use parking_lot::RwLock as PRwLock; use parking_lot::RwLock as PRwLock;
@@ -21,7 +22,7 @@ use serenity::{
voice::VoiceState, voice::VoiceState,
}, },
}; };
use std::{collections::HashMap, sync::Arc}; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
#[cfg(feature = "twilight")] #[cfg(feature = "twilight")]
use twilight_gateway::Cluster; use twilight_gateway::Cluster;
@@ -44,7 +45,7 @@ struct ClientData {
#[derive(Debug)] #[derive(Debug)]
pub struct Songbird { pub struct Songbird {
client_data: PRwLock<ClientData>, client_data: PRwLock<ClientData>,
calls: PRwLock<HashMap<GuildId, Arc<Mutex<Call>>>>, calls: DashMap<GuildId, Arc<Mutex<Call>>>,
sharder: Sharder, sharder: Sharder,
#[cfg(feature = "driver")] #[cfg(feature = "driver")]
@@ -117,8 +118,9 @@ impl Songbird {
/// ///
/// [`Call`]: Call /// [`Call`]: Call
pub fn get<G: Into<GuildId>>(&self, guild_id: G) -> Option<Arc<Mutex<Call>>> { pub fn get<G: Into<GuildId>>(&self, guild_id: G) -> Option<Arc<Mutex<Call>>> {
let map_read = self.calls.read(); self.calls
map_read.get(&guild_id.into()).cloned() .get(&guild_id.into())
.map(|mapref| Arc::clone(&mapref))
} }
/// Retrieves a [`Call`] for the given guild, creating a new one if /// Retrieves a [`Call`] for the given guild, creating a new one if
@@ -129,9 +131,7 @@ impl Songbird {
/// [`Call`]: Call /// [`Call`]: Call
pub fn get_or_insert(&self, guild_id: GuildId) -> Arc<Mutex<Call>> { pub fn get_or_insert(&self, guild_id: GuildId) -> Arc<Mutex<Call>> {
self.get(guild_id).unwrap_or_else(|| { self.get(guild_id).unwrap_or_else(|| {
let mut map_read = self.calls.write(); self.calls
map_read
.entry(guild_id) .entry(guild_id)
.or_insert_with(|| { .or_insert_with(|| {
let info = self.manager_info(); let info = self.manager_info();
@@ -301,8 +301,7 @@ impl Songbird {
async fn _remove(&self, guild_id: GuildId) -> JoinResult<()> { async fn _remove(&self, guild_id: GuildId) -> JoinResult<()> {
self.leave(guild_id).await?; self.leave(guild_id).await?;
let mut calls = self.calls.write(); self.calls.remove(&guild_id);
calls.remove(&guild_id);
Ok(()) Ok(())
} }
} }