Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 9 additions & 9 deletions intelligence/company/meridian_platform/status_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from __future__ import annotations

import concurrent.futures
import copy
import datetime
import json
import os
import threading
import time
Expand Down Expand Up @@ -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(
Expand All @@ -490,17 +490,17 @@ 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)
if not isinstance(cached_entry, dict):
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:
Expand Down Expand Up @@ -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

Expand Down
Loading