Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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`.
16 changes: 8 additions & 8 deletions intelligence/company/meridian_platform/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 8 additions & 8 deletions kernel/kernel/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading