mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-21 19:38:49 +00:00
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:
@@ -62,7 +62,7 @@ impl super::ChargingEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn apply_charging_rules(&self, event: &UsageEvent) -> ChargingResult<Vec<ChargingRule>> {
|
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
|
// Rule 1: Check if user has sufficient credit
|
||||||
if !self.check_credit(&event.imsi, event.volume).await? {
|
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()))?;
|
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
|
||||||
|
|
||||||
let pattern = format!("usage:{}:*", imsi);
|
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
|
// Use SCAN instead of KEYS to avoid blocking Redis
|
||||||
let mut cursor = 0u64;
|
let mut cursor = 0u64;
|
||||||
@@ -216,7 +216,7 @@ impl super::ChargingEngine {
|
|||||||
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
|
.map_err(|e| crate::errors::ChargingError::RedisConnection(e.to_string()))?;
|
||||||
|
|
||||||
let pattern = "account:*".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
|
// Use SCAN instead of KEYS to avoid blocking Redis
|
||||||
let mut cursor = 0u64;
|
let mut cursor = 0u64;
|
||||||
|
|||||||
Reference in New Issue
Block a user