diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..e24b17b --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-05-10 - Optimization for Observability Cache +**Learning:** Using copy.deepcopy on parsed dictionaries (like observability snapshots) is slow compared to caching the raw JSON string and parsing it on demand with json.loads. +**Action:** Replace copy.deepcopy in observability_snapshot caching with json.loads(json.dumps(snapshot)) pattern where json string is cached, or just json.dumps at cache write and json.loads at read. But we have memory constraint, wait, memory says: 'avoid using copy.deepcopy() on the parsed dictionary as it is often slower than reading from the OS page cache. Instead, cache the raw JSON string and parse it on demand with json.loads(cached_str).' So we should cache json string. diff --git a/intelligence/company/meridian_platform/status_surface.py b/intelligence/company/meridian_platform/status_surface.py index f241152..8d94bb0 100644 --- a/intelligence/company/meridian_platform/status_surface.py +++ b/intelligence/company/meridian_platform/status_surface.py @@ -4,8 +4,8 @@ from __future__ import annotations import concurrent.futures -import copy import datetime +import json import os import threading import time @@ -473,15 +473,15 @@ def observability_snapshot(org_id, *, record_alerts=True): payload = cached_entry.get('payload') refresh_future = cached_entry.get('refresh_future') if ( - isinstance(payload, dict) + isinstance(payload, str) and fetched_at > 0 and (now - fetched_at) <= ttl_seconds ): - return copy.deepcopy(payload) - if not isinstance(payload, dict): + return json.loads(payload) + if not isinstance(payload, str): pass # cold start — fall through to synchronous build below elif refresh_future and not refresh_future.done(): - return copy.deepcopy(payload) + return json.loads(payload) else: if not (refresh_future and not refresh_future.done()): refresh_future = OBSERVABILITY_SNAPSHOT_EXECUTOR.submit( @@ -490,9 +490,9 @@ def observability_snapshot(org_id, *, record_alerts=True): record_alerts=record_alerts, ) cached_entry['refresh_future'] = refresh_future - return copy.deepcopy(payload) + return json.loads(payload) - if not isinstance(payload, dict): + if not isinstance(payload, str): snapshot = _build_observability_snapshot(org_id, record_alerts=record_alerts) with OBSERVABILITY_SNAPSHOT_CACHE_LOCK: cached_entry = OBSERVABILITY_SNAPSHOT_CACHE.get(cache_key) @@ -500,7 +500,7 @@ def observability_snapshot(org_id, *, record_alerts=True): cached_entry = {'fetched_at': 0.0, 'payload': None, 'refresh_future': None} OBSERVABILITY_SNAPSHOT_CACHE[cache_key] = cached_entry cached_entry['fetched_at'] = time.time() - cached_entry['payload'] = copy.deepcopy(snapshot) + cached_entry['payload'] = json.dumps(snapshot) cached_entry['refresh_future'] = None return snapshot try: @@ -603,7 +603,7 @@ def observability_snapshot(org_id, *, record_alerts=True): cached_entry = {'fetched_at': 0.0, 'payload': None, 'refresh_future': None} OBSERVABILITY_SNAPSHOT_CACHE[cache_key] = cached_entry cached_entry['fetched_at'] = time.time() - cached_entry['payload'] = copy.deepcopy(snapshot) + cached_entry['payload'] = json.dumps(snapshot) cached_entry['refresh_future'] = None return snapshot