Skip to content

Commit

Permalink
chore: optimize regex usage, avoid useless recreation of Regex instan…
Browse files Browse the repository at this point in the history
…ce. It saves a few seconds in boot (I should really think about a method to save a few minutes, not seconds..)
  • Loading branch information
RakuJa committed Jul 7, 2024
1 parent 071477f commit 710fc64
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dotenvy = "0.15.7"

env_logger = "0.11.3"
log = "0.4.22"
once_cell = "1.19.0"

[build-dependencies]
tokio = { version = "1", features = ["macros", "rt-multi-thread", "rt"] }
Expand Down
56 changes: 16 additions & 40 deletions src/models/creature/creature_metadata/creature_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::models::creature::creature_component::creature_spell_caster::Creature
use crate::models::item::item_metadata::type_enum::WeaponTypeEnum;
use crate::models::scales_struct::creature_scales::CreatureScales;
use num_traits::float::FloatConst;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
Expand All @@ -30,6 +31,12 @@ pub enum CreatureRoleEnum {
SpellCaster,
}

fn get_dmg_from_regex(raw_str: &str) -> Option<i64> {
// It will only initialize it once.
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\((\d+)\)").unwrap());
RE.captures(raw_str)?.get(1)?.as_str().parse::<i64>().ok()
}

impl CreatureRoleEnum {
pub fn to_db_column(&self) -> String {
match self {
Expand All @@ -49,17 +56,16 @@ impl CreatureRoleEnum {
cr_spells: &CreatureSpellCasterData,
scales: &CreatureScales,
) -> BTreeMap<CreatureRoleEnum, i64> {
let dmg_scales_regex = Regex::new(r"\((\d+)\)").unwrap();
let mut roles = BTreeMap::new();
roles.insert(
Self::Brute,
is_brute(cr_core, cr_extra, cr_combat, scales, &dmg_scales_regex)
is_brute(cr_core, cr_extra, cr_combat, scales)
.map(|x| (x * 100.).round() as i64)
.unwrap_or(0),
);
roles.insert(
Self::MagicalStriker,
is_magical_striker(cr_core, cr_spells, cr_combat, scales, &dmg_scales_regex)
is_magical_striker(cr_core, cr_spells, cr_combat, scales)
.map(|x| (x * 100.).round() as i64)
.unwrap_or(0),
);
Expand All @@ -77,13 +83,13 @@ impl CreatureRoleEnum {
);
roles.insert(
Self::Sniper,
is_sniper(cr_core, cr_extra, cr_combat, scales, &dmg_scales_regex)
is_sniper(cr_core, cr_extra, cr_combat, scales)
.map(|x| (x * 100.).round() as i64)
.unwrap_or(0),
);
roles.insert(
Self::Soldier,
is_soldier(cr_core, cr_extra, cr_combat, scales, &dmg_scales_regex)
is_soldier(cr_core, cr_extra, cr_combat, scales)
.map(|x| (x * 100.).round() as i64)
.unwrap_or(0),
);
Expand All @@ -106,7 +112,6 @@ fn is_brute(
cr_extra: &CreatureExtraData,
cr_combat: &CreatureCombatData,
scales: &CreatureScales,
re: &Regex,
) -> Option<f64> {
let mut score: u16 = 0;
let lvl = cr_core.level;
Expand Down Expand Up @@ -145,18 +150,8 @@ fn is_brute(
let atk_bonus_scales = scales.strike_bonus_scales.get(&lvl)?;
let dmg_scales = scales.strike_dmg_scales.get(&lvl)?;

let scales_extreme_avg = re
.captures(dmg_scales.extreme.as_str())?
.get(1)?
.as_str()
.parse::<i64>()
.ok()?;
let scales_high_avg = re
.captures(dmg_scales.high.as_str())?
.get(1)?
.as_str()
.parse::<i64>()
.ok()?;
let scales_extreme_avg = get_dmg_from_regex(dmg_scales.extreme.as_str())?;
let scales_high_avg = get_dmg_from_regex(dmg_scales.high.as_str())?;
// high attack bonus and high damage OR moderate attack bonus and extreme damage
let wp_distance = cr_combat
.weapons
Expand Down Expand Up @@ -187,7 +182,6 @@ fn is_sniper(
cr_extra: &CreatureExtraData,
cr_combat: &CreatureCombatData,
scales: &CreatureScales,
re: &Regex,
) -> Option<f64> {
let mut score: u16 = 0;
let lvl = cr_core.level;
Expand All @@ -208,12 +202,7 @@ fn is_sniper(
// moderate to low HP; skipped
let atk_bonus_scales = scales.strike_bonus_scales.get(&lvl)?;
let dmg_scales = scales.strike_dmg_scales.get(&lvl)?;
let scales_mod_avg = re
.captures(dmg_scales.moderate.as_str())?
.get(1)?
.as_str()
.parse::<i64>()
.ok()?;
let scales_mod_avg = get_dmg_from_regex(dmg_scales.moderate.as_str())?;
// ranged Strikes have high attack bonus and damage or
// moderate attack bonus and extreme damage (melee Strikes are weaker)
let wp_distance = cr_combat
Expand Down Expand Up @@ -268,7 +257,6 @@ pub fn is_soldier(
cr_extra: &CreatureExtraData,
cr_combat: &CreatureCombatData,
scales: &CreatureScales,
re: &Regex,
) -> Option<f64> {
let mut score: u16 = 0;
let lvl = cr_core.level;
Expand All @@ -283,14 +271,8 @@ pub fn is_soldier(
score += calculate_lb_distance(saving_scales.high, cr_combat.saving_throws.fortitude);
let atk_bonus_scales = scales.strike_bonus_scales.get(&lvl)?;
let dmg_scales = scales.strike_dmg_scales.get(&lvl)?;
let scales_high_avg = re
.captures(dmg_scales.high.as_str())?
.get(1)?
.as_str()
.parse::<i64>()
.ok()?;
let scales_high_avg = get_dmg_from_regex(dmg_scales.high.as_str())?;
// high attack bonus and high damage;

let wp_distance = cr_combat
.weapons
.iter()
Expand Down Expand Up @@ -326,18 +308,12 @@ pub fn is_magical_striker(
cr_spell: &CreatureSpellCasterData,
cr_combat: &CreatureCombatData,
scales: &CreatureScales,
re: &Regex,
) -> Option<f64> {
let mut score: u16 = 0;
let lvl = cr_core.level;
let atk_bonus_scales = scales.strike_bonus_scales.get(&lvl)?;
let dmg_scales = scales.strike_dmg_scales.get(&lvl)?;
let scales_high_avg = re
.captures(dmg_scales.high.as_str())?
.get(1)?
.as_str()
.parse::<i64>()
.ok()?;
let scales_high_avg = get_dmg_from_regex(dmg_scales.high.as_str())?;
// high attack bonus and high damage;
let wp_distance = cr_combat
.weapons
Expand Down

0 comments on commit 710fc64

Please sign in to comment.