diff --git a/.jules/bolt.md b/.jules/bolt.md index c717282b..54b216a1 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -19,3 +19,7 @@ ## 2024-05-24 - Avoid Copying Large Sets for Membership Checks **Learning:** Copying a large set (e.g. 100k items) to create a snapshot for read-only membership checks is expensive O(N) and unnecessary. Python's set membership testing is thread-safe. **Action:** When filtering data against a shared large set, iterate and check membership directly instead of snapshotting, unless strict transactional consistency across the entire iteration is required. + +## 2024-05-24 - Minimize Lock Contention +**Learning:** In parallel operations updating shared state, preparing data (parsing, filtering) inside a lock forces serialization of CPU-bound work. Moving data preparation outside the lock minimizes the critical section duration. +**Action:** Collect/prepare data in local variables first, then update shared state in a minimal critical section (e.g. using `set.update`). diff --git a/main.py b/main.py index e6aabc57..600393a2 100644 --- a/main.py +++ b/main.py @@ -333,10 +333,13 @@ def _fetch_folder_rules(folder_id: str): try: data = _api_get(client, f"{API_BASE}/{profile_id}/rules/{folder_id}").json() folder_rules = data.get("body", {}).get("rules", []) - with all_rules_lock: - for rule in folder_rules: - if rule.get("PK"): - all_rules.add(rule["PK"]) + + # Optimization: Collect local rules first to minimize lock contention + local_rules = {rule["PK"] for rule in folder_rules if rule.get("PK")} + + if local_rules: + with all_rules_lock: + all_rules.update(local_rules) except httpx.HTTPError: pass except Exception as e: