From 4b15de794990bd226ea82247a8f3d0d82e218c8d Mon Sep 17 00:00:00 2001 From: scottbot95 Date: Mon, 10 Jul 2023 13:37:40 -0700 Subject: [PATCH] Convert Cooldowns to use HashMap instead of OrderedMap for better scaling (#180) --- src/cooldown.rs | 20 ++++++++++---------- src/util.rs | 2 ++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/cooldown.rs b/src/cooldown.rs index 0795744c50b1..34a01d54d9e5 100644 --- a/src/cooldown.rs +++ b/src/cooldown.rs @@ -2,7 +2,7 @@ use crate::serenity_prelude as serenity; // I usually don't really do imports, but these are very convenient -use crate::util::OrderedMap; +use std::collections::HashMap; use std::time::{Duration, Instant}; /// Subset of [`crate::Context`] so that [`Cooldowns`] can be used without requiring a full [Context](`crate::Context`) @@ -38,18 +38,18 @@ pub struct CooldownConfig { /// /// You probably don't need to use this directly. `#[poise::command]` automatically generates a /// cooldown handler. -#[derive(Default, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Default, Clone, Debug, PartialEq, Eq)] pub struct CooldownTracker { /// Stores the timestamp of the last global invocation global_invocation: Option, /// Stores the timestamps of the last invocation per user - user_invocations: OrderedMap, + user_invocations: HashMap, /// Stores the timestamps of the last invocation per guild - guild_invocations: OrderedMap, + guild_invocations: HashMap, /// Stores the timestamps of the last invocation per channel - channel_invocations: OrderedMap, + channel_invocations: HashMap, /// Stores the timestamps of the last invocation per member (user and guild) - member_invocations: OrderedMap<(serenity::UserId, serenity::GuildId), Instant>, + member_invocations: HashMap<(serenity::UserId, serenity::GuildId), Instant>, } /// **Renamed to [`CooldownTracker`]** @@ -60,10 +60,10 @@ impl CooldownTracker { pub fn new() -> Self { Self { global_invocation: None, - user_invocations: OrderedMap::new(), - guild_invocations: OrderedMap::new(), - channel_invocations: OrderedMap::new(), - member_invocations: OrderedMap::new(), + user_invocations: HashMap::new(), + guild_invocations: HashMap::new(), + channel_invocations: HashMap::new(), + member_invocations: HashMap::new(), } } diff --git a/src/util.rs b/src/util.rs index c5199c92e987..3c614bfa87d0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -17,6 +17,7 @@ impl OrderedMap { } /// Finds a value in the map by the given key + #[allow(dead_code)] pub fn get(&self, k: &K) -> Option<&V> { self.0 .iter() @@ -25,6 +26,7 @@ impl OrderedMap { } /// Inserts a key value pair into the map + #[allow(dead_code)] pub fn insert(&mut self, k: K, v: V) { match self.0.iter_mut().find(|entry| entry.0 == k) { Some(entry) => entry.1 = v,