diff --git a/.jules/bolt.md b/.jules/bolt.md index 02a6e1a2..6093337e 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -85,3 +85,7 @@ ## 2026-05-16 - Pre-processing for RAG Retrieval **Learning:** In RAG (Retrieval-Augmented Generation) systems with static or semi-static policy datasets, performing tokenization, regex substitution, and string formatting inside the retrieval loop is a significant bottleneck that scales with the number of policies. **Action:** Move all deterministic operations (tokenization, formatting, regex matching prep) to a one-time initialization step to ensure the retrieval hot-path only performs necessary set intersections and similarity calculations. + +## 2026-05-17 - Redundant Configuration I/O in Service Initialization +**Learning:** Instantiating services that read static configuration files from disk on every `__init__` call is a significant performance bottleneck, especially in high-traffic or frequent background task scenarios. This adds unnecessary latency and disk I/O on every request or task execution. +**Action:** Implement class-level or module-level caching for static configuration files to ensure they are only read and parsed once per process lifetime, significantly reducing initialization overhead. diff --git a/backend/grievance_service.py b/backend/grievance_service.py index 01218d9b..36515e9a 100644 --- a/backend/grievance_service.py +++ b/backend/grievance_service.py @@ -22,6 +22,9 @@ class GrievanceService: Main service for managing grievances, routing, and escalations. """ + # Class-level cache to avoid redundant disk I/O when instantiating the service + _rules_cache = {} + def __init__(self, rules_config_path: str = "backend/grievance_rules.json"): """ Initialize the grievance service. @@ -29,8 +32,12 @@ def __init__(self, rules_config_path: str = "backend/grievance_rules.json"): Args: rules_config_path: Path to the rules configuration file """ - with open(rules_config_path, 'r') as f: - self.rules_config = json.load(f) + # Optimized: Use class-level cache to avoid reading and parsing the JSON file repeatedly + if rules_config_path not in GrievanceService._rules_cache: + with open(rules_config_path, 'r') as f: + GrievanceService._rules_cache[rules_config_path] = json.load(f) + + self.rules_config = GrievanceService._rules_cache[rules_config_path] self.routing_service = RoutingService(self.rules_config) self.sla_service = SLAConfigService(