refactor: Pre-allocate Vec capacity in rating_billing.rs to reduce reallocations

- Set applied_rules capacity to 6 (maximum number of charging rules that can be applied)
- Set keys capacity to 100 in get_usage_history and get_all_accounts (matches SCAN batch size)
- Add comments explaining capacity choices for each Vec initialization
This commit is contained in:
nutcas3
2026-04-29 04:24:22 +03:00
parent 5ec01e591e
commit 4008a19f4a
@@ -62,7 +62,7 @@ impl super::ChargingEngine {
}
pub async fn apply_charging_rules(&self, event: &UsageEvent) -> ChargingResult<Vec<ChargingRule>> {
let mut applied_rules = Vec::new();
let mut applied_rules = Vec::with_capacity(6); // Max ~6 rules can be applied
// Rule 1: Check if user has sufficient credit
if !self.check_credit(&event.imsi, event.volume).await? {
@@ -159,7 +159,7 @@ impl super::ChargingEngine {
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
let pattern = format!("usage:{}:*", imsi);
let mut keys: Vec<String> = Vec::new();
let mut keys: Vec<String> = Vec::with_capacity(100); // SCAN batch size
// Use SCAN instead of KEYS to avoid blocking Redis
let mut cursor = 0u64;
@@ -216,7 +216,7 @@ impl super::ChargingEngine {
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
let pattern = "account:*".to_string();
let mut keys: Vec<String> = Vec::new();
let mut keys: Vec<String> = Vec::with_capacity(100); // SCAN batch size
// Use SCAN instead of KEYS to avoid blocking Redis
let mut cursor = 0u64;