From dad880bdbbc9b9b4ba992a933110b28ae32c90a2 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 10 May 2026 19:04:04 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Replace=20copy.deepcopy=20w?= =?UTF-8?q?ith=20json=20caching=20for=20observability=20snapshot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces the slow `copy.deepcopy` logic inside `observability_snapshot` with a much faster `json.loads(json.dumps(...))` pattern for caching the large JSON-serializable observability data in memory. This significantly decreases latency when multiple reads or background tasks trigger dictionary duplications. Co-authored-by: mapleleaflatte03 <240846662+mapleleaflatte03@users.noreply.github.com> --- .jules/bolt.md | 3 +++ .../meridian_platform/status_surface.py | 18 +++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..e24b17b7 --- /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 f2411524..8d94bb0d 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