From 4008a19f4aba132c1682b72e0d0a02c3788038e4 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Wed, 29 Apr 2026 04:24:22 +0300 Subject: [PATCH] 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 --- apps/charging-engine/src/charging/rating_billing.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/charging-engine/src/charging/rating_billing.rs b/apps/charging-engine/src/charging/rating_billing.rs index 4080ce1..824dbd5 100644 --- a/apps/charging-engine/src/charging/rating_billing.rs +++ b/apps/charging-engine/src/charging/rating_billing.rs @@ -62,7 +62,7 @@ impl super::ChargingEngine { } pub async fn apply_charging_rules(&self, event: &UsageEvent) -> ChargingResult> { - 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 = Vec::new(); + let mut keys: Vec = 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 = Vec::new(); + let mut keys: Vec = Vec::with_capacity(100); // SCAN batch size // Use SCAN instead of KEYS to avoid blocking Redis let mut cursor = 0u64;