From 6abb38f3ebf9537ec53ef0c5c9e5852ff57cf479 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 26 Mar 2026 19:10:43 +0000 Subject: [PATCH] Optimize save_items in JsonFilePracticeRepository to modify dictionaries in-place Refactored `JsonFilePracticeRepository.save_items` to avoid fully deserializing and re-serializing the entire collection of stored items during a batch save. The method now updates the raw dictionary entries directly and only serializes the updated/new items, significantly reducing CPU overhead during batch DB operations. 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..4425990 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. + +## 2024-05-25 - [Optimize batch DB operations by avoiding full deserialization] +**Learning:** Fully deserializing and reserializing stored records to domain models for every batch update incurs O(N) CPU overhead, even when only a small subset of records change. +**Action:** To optimize JSON file-backed adapters, modify raw storage dictionary entries in-place for existing items and append new entries, bypassing unnecessary full-collection object instantiation. 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..f563140 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 = [] + + new_items_by_id = {str(item.id): _item_to_dict(item) for item in items} + + for i in range(len(raw_items)): + entry = raw_items[i] + if isinstance(entry, dict): + entry_id = str(entry.get("id")) + if entry_id in new_items_by_id: + raw_items[i] = new_items_by_id.pop(entry_id) + if not new_items_by_id: + break + + for new_item_dict in new_items_by_id.values(): + raw_items.append(new_item_dict) + + storage["items"] = raw_items self._save_storage(storage) def list_attempts(self) -> list[Attempt]: