From 8148d5a67f9802e871219bd8509a9ca68164380c Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 2 May 2026 19:12:25 +0000 Subject: [PATCH] Refactor blocked case queries to use set instead of array inclusion Co-authored-by: mapleleaflatte03 <240846662+mapleleaflatte03@users.noreply.github.com> --- .jules/bolt.md | 3 +++ intelligence/company/meridian_platform/cases.py | 16 ++++++++-------- kernel/kernel/cases.py | 16 ++++++++-------- 3 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..95d78636 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-02 - O(N^2) Anti-Pattern in Unique Value Extraction +**Learning:** O(N) inclusion checks (`not in seen` where `seen` is a list) inside an O(N) loop when filtering uniqueness out of cases records in `blocking_commitment_ids` and `blocked_peer_host_ids` scales quadratically O(N^2) and is noticeably slower even on small N due to repeated list scans. `cases.py` functions use this pattern, limiting performance as the number of cases grows. +**Action:** Always use sets (`seen = set()`) for O(1) inclusion checks and uniqueness constraints when accumulating IDs or references from loops, then convert the set back to a sorted list (`sorted(list(seen))` or `sorted(seen)`) at the end. I will apply this to `kernel/kernel/cases.py` and `intelligence/company/meridian_platform/cases.py` for `blocking_commitment_ids` and `blocked_peer_host_ids`. diff --git a/intelligence/company/meridian_platform/cases.py b/intelligence/company/meridian_platform/cases.py index 9e9730e7..a51e4d5c 100644 --- a/intelligence/company/meridian_platform/cases.py +++ b/intelligence/company/meridian_platform/cases.py @@ -230,21 +230,21 @@ def peer_can_be_thawed(peer_host_id, org_id=None, *, claim_types=None): def blocking_commitment_ids(org_id=None): - seen = [] + seen = set() for row in blocking_cases(org_id): commitment_id = (row.get('linked_commitment_id') or '').strip() - if commitment_id and commitment_id not in seen: - seen.append(commitment_id) - return sorted(seen) + if commitment_id: + seen.add(commitment_id) + return sorted(list(seen)) def blocked_peer_host_ids(org_id=None): - seen = [] + seen = set() for row in blocking_cases(org_id): peer_host_id = (row.get('target_host_id') or '').strip() - if peer_host_id and case_requires_peer_block(row) and peer_host_id not in seen: - seen.append(peer_host_id) - return sorted(seen) + if peer_host_id and case_requires_peer_block(row): + seen.add(peer_host_id) + return sorted(list(seen)) def _commitment_counterparty_binding(commitment_record): diff --git a/kernel/kernel/cases.py b/kernel/kernel/cases.py index 07f935a1..38332329 100644 --- a/kernel/kernel/cases.py +++ b/kernel/kernel/cases.py @@ -309,21 +309,21 @@ def blocking_peer_case(peer_host_id, org_id=None, *, claim_types=None): def blocking_commitment_ids(org_id=None): - seen = [] + seen = set() for row in blocking_cases(org_id): commitment_id = (row.get('linked_commitment_id') or '').strip() - if commitment_id and commitment_id not in seen: - seen.append(commitment_id) - return sorted(seen) + if commitment_id: + seen.add(commitment_id) + return sorted(list(seen)) def blocked_peer_host_ids(org_id=None): - seen = [] + seen = set() for row in blocking_cases(org_id): peer_host_id = (row.get('target_host_id') or '').strip() - if peer_host_id and case_requires_peer_block(row) and peer_host_id not in seen: - seen.append(peer_host_id) - return sorted(seen) + if peer_host_id and case_requires_peer_block(row): + seen.add(peer_host_id) + return sorted(list(seen)) def _commitment_counterparty_binding(commitment_record):