From 307a103772af5f730c0bc0d62573987fd0876e3f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 27 Mar 2026 18:56:12 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20JsonFilePractice?= =?UTF-8?q?Repository.save=5Fitems=20with=20in-place=20updates?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ivangegovdve-sudo <225339531+ivangegovdve-sudo@users.noreply.github.com> --- .jules/bolt.md | 4 +++ .../adapters/json_file_practice_repository.py | 27 ++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 699e7a3..1df856d 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -13,3 +13,7 @@ ## 2024-03-16 - Batch JSON File I/O Operations **Learning:** When using JSON file-backed repositories, iterating over items sequentially and calling `save_item` or `record_attempt` inside a loop leads to N+1 file read/write operations. This creates a significant performance bottleneck, especially when importing progress snapshots with numerous items and attempts. **Action:** Prefer batch processing methods (e.g., `save_items`, `record_attempts`) so file-backed adapters can load storage once, update it in memory, and write it back in a single pass. + +## 2025-03-27 - Batch JSON File I/O Updates +**Learning:** In JSON file-backed adapters, deserializing all stored records to domain models and then reserializing them inside `save_items` causes an O(N) CPU/memory penalty. +**Action:** When updating JSON storage for batch methods like `save_items`, always update the raw dictionary entries in-place. diff --git a/src/python_learning_orchestrated/adapters/json_file_practice_repository.py b/src/python_learning_orchestrated/adapters/json_file_practice_repository.py index 15da61b..95f1743 100644 --- a/src/python_learning_orchestrated/adapters/json_file_practice_repository.py +++ b/src/python_learning_orchestrated/adapters/json_file_practice_repository.py @@ -46,15 +46,24 @@ def save_items(self, items: list[LearningItem]) -> None: storage = self._load_storage() raw_items = storage.get("items", []) - existing_items = [] - if isinstance(raw_items, list): - existing_items = [ - _item_from_dict(entry) for entry in raw_items if isinstance(entry, dict) - ] - by_id = {existing.id: existing for existing in existing_items} - for item in items: - by_id[item.id] = item - storage["items"] = [_item_to_dict(entry) for entry in by_id.values()] + if not isinstance(raw_items, list): + raw_items = [] + + # ⚡ Bolt: Optimize batch saves by updating raw dicts in-place + # instead of full deserialize/serialize loop (avoids O(N) penalty) + new_items_by_id = {str(item.id): _item_to_dict(item) for item in items} + + for entry in raw_items: + if isinstance(entry, dict): + entry_id = str(entry.get("id")) + if entry_id in new_items_by_id: + entry.clear() + entry.update(new_items_by_id.pop(entry_id)) + + for new_item in new_items_by_id.values(): + raw_items.append(new_item) + + storage["items"] = raw_items self._save_storage(storage) def list_attempts(self) -> list[Attempt]: