From 36e29abce07804fd779adf99d3a7535253d39732 Mon Sep 17 00:00:00 2001 From: Ushie Date: Mon, 15 May 2023 01:44:20 +0300 Subject: [PATCH] perf: lazy init censor --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/commands/configuration.rs | 3 ++ src/utils/decancer.rs | 65 ++++++++++++++++++++++++----------- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8be4dd6..1246ba5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1366,7 +1366,9 @@ dependencies = [ "dirs", "dotenv", "hmac-sha256", + "lazy_static", "mongodb", + "once_cell", "parse_duration", "poise", "regex", diff --git a/Cargo.toml b/Cargo.toml index e690f0a..734d9ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,3 +36,5 @@ hmac-sha256 = "1.1.6" base64 = "0.21.0" parse_duration = "2.1.1" censor = "0.3.0" +lazy_static = "1.4.0" +once_cell = "1.17.1" diff --git a/src/commands/configuration.rs b/src/commands/configuration.rs index 6d69a28..c95177d 100644 --- a/src/commands/configuration.rs +++ b/src/commands/configuration.rs @@ -1,6 +1,7 @@ use tracing::debug; use crate::utils::bot::load_configuration; +use crate::utils::decancer::reinit_censor; use crate::{Context, Error}; /// Reload the Discord bot. @@ -13,6 +14,8 @@ pub async fn reload(ctx: Context<'_>) -> Result<(), Error> { // Also save the new configuration to the user data ctx.data().write().await.configuration = configuration; + reinit_censor(ctx.serenity_context()).await; + debug!("{} reloaded the configuration.", ctx.author().name); ctx.send(|f| { diff --git a/src/utils/decancer.rs b/src/utils/decancer.rs index 4110acd..5c4d746 100644 --- a/src/utils/decancer.rs +++ b/src/utils/decancer.rs @@ -1,9 +1,44 @@ -extern crate decancer; - -use censor::*; +use censor::Censor; +use lazy_static::lazy_static; +use once_cell::sync::OnceCell; use tracing::{error, info, trace}; -use super::{*, bot::get_data_lock}; +use super::{bot, serenity}; + +lazy_static! { + static ref CENSOR: OnceCell = OnceCell::new(); +} + +// Initialize the censor +async fn censor(ctx: &serenity::Context) -> &'static Censor { + let data_lock = bot::get_data_lock(ctx).await; + let censor_config = &data_lock.read().await.configuration.general.censor; + let additions = &censor_config.additions; + let removals = &censor_config.removals; + + CENSOR.get_or_init(|| { + let mut censor = censor::Standard; + + for addition in additions { + censor += addition; + } + + for removal in removals { + censor -= removal; + } + + censor + }) +} + +// Reinitialize the censor when the configuration is reloaded +pub async fn reinit_censor(ctx: &serenity::Context) { + match CENSOR.set(censor(ctx).await.clone()) { + Ok(_) => info!("Reinitialized censor"), + Err(_) => error!("Failed to reinitialize censor"), + } +} + pub async fn cure( ctx: &serenity::Context, @@ -14,21 +49,8 @@ pub async fn cure( trace!("Skipping decancer for bot {}.", member.user.tag()); return; } - - let data_lock = get_data_lock(ctx).await; - let censor = &data_lock.read().await.configuration.general.censor; - let additions = &censor.additions; - let removals = &censor.removals; - - let mut censor = Standard; - - for addition in additions { - censor += addition; - } - - for removal in removals { - censor -= removal; - } + + let censor = censor(ctx).await; let name = member.display_name().to_string(); @@ -47,7 +69,10 @@ pub async fn cure( "", ); - if cured_name.is_empty() || !cured_name.starts_with(|c: char| c.is_ascii_alphabetic()) || censor.check(&cured_name) { + if cured_name.is_empty() + || !cured_name.starts_with(|c: char| c.is_ascii_alphabetic()) + || censor.check(&cured_name) + { cured_name = "ReVanced member".to_string(); }