Skip to content

Commit

Permalink
Convert Cooldowns to use HashMap instead of OrderedMap for better sca…
Browse files Browse the repository at this point in the history
…ling (serenity-rs#180)
  • Loading branch information
scottbot95 authored and GnomedDev committed Nov 26, 2023
1 parent bd73861 commit 4b15de7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/cooldown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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<Instant>,
/// Stores the timestamps of the last invocation per user
user_invocations: OrderedMap<serenity::UserId, Instant>,
user_invocations: HashMap<serenity::UserId, Instant>,
/// Stores the timestamps of the last invocation per guild
guild_invocations: OrderedMap<serenity::GuildId, Instant>,
guild_invocations: HashMap<serenity::GuildId, Instant>,
/// Stores the timestamps of the last invocation per channel
channel_invocations: OrderedMap<serenity::ChannelId, Instant>,
channel_invocations: HashMap<serenity::ChannelId, Instant>,
/// 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`]**
Expand All @@ -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(),
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl<K: Eq, V> OrderedMap<K, V> {
}

/// Finds a value in the map by the given key
#[allow(dead_code)]
pub fn get(&self, k: &K) -> Option<&V> {
self.0
.iter()
Expand All @@ -25,6 +26,7 @@ impl<K: Eq, V> OrderedMap<K, V> {
}

/// 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,
Expand Down

0 comments on commit 4b15de7

Please sign in to comment.