-
Notifications
You must be signed in to change notification settings - Fork 35
⚡ Bolt: Cache grievance rules to avoid redundant disk I/O #701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,15 +22,22 @@ 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. | ||
|
|
||
| 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] | ||
|
Comment on lines
+25
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cached
Additionally, Ruff RUF012 flags Two reasonable fixes (pick one): ♻️ Option A — deep-copy on read (preserves isolation, parsed once)-import json
+import copy
+import json
@@
- # Class-level cache to avoid redundant disk I/O when instantiating the service
- _rules_cache = {}
+ # Class-level cache of parsed rules JSON, keyed by config path, to avoid
+ # redundant disk I/O across GrievanceService instantiations.
+ _rules_cache: Dict[str, Dict[str, Any]] = {}
@@
- # 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]
+ # Cache the parsed JSON once per path; deep-copy on read so per-instance
+ # mutations cannot leak into the shared cache or other instances.
+ if rules_config_path not in GrievanceService._rules_cache:
+ with open(rules_config_path, 'r', encoding='utf-8') as f:
+ GrievanceService._rules_cache[rules_config_path] = json.load(f)
+
+ self.rules_config = copy.deepcopy(GrievanceService._rules_cache[rules_config_path])♻️ Option B — cache the raw JSON string and re-parse (simpler, still ~order-of-magnitude faster than disk I/O)- # Class-level cache to avoid redundant disk I/O when instantiating the service
- _rules_cache = {}
+ # Class-level cache of raw JSON text, keyed by path, to avoid redundant disk I/O.
+ _rules_cache: Dict[str, str] = {}
@@
- 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]
+ if rules_config_path not in GrievanceService._rules_cache:
+ with open(rules_config_path, 'r', encoding='utf-8') as f:
+ GrievanceService._rules_cache[rules_config_path] = f.read()
+
+ self.rules_config = json.loads(GrievanceService._rules_cache[rules_config_path])If you can guarantee that no downstream consumer ever mutates 🧰 Tools🪛 Ruff (0.15.11)[warning] 26-26: Mutable default value for class attribute (RUF012) 🤖 Prompt for AI Agents
Comment on lines
+35
to
+40
|
||
|
|
||
| self.routing_service = RoutingService(self.rules_config) | ||
| self.sla_service = SLAConfigService( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_rules_cacheis a process-wide mutable dict that is populated via a check-then-set sequence without any synchronization. If this service can be instantiated concurrently (threads/background jobs), consider guarding cache population with a lock or using the existingThreadSafeCacheutility to avoid concurrent loads and future race-prone mutations.