diff --git a/.jules/bolt.md b/.jules/bolt.md index 341c7c91..6efd4673 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -67,3 +67,6 @@ ## 2025-02-12 - [Fast Path Execution in Directory Traversal and Log Parsing] **Learning:** Checking for string existence (`if "silence_" not in stderr`) before invoking regex matchers provides significant speed improvements when parsing large blocks of text. Similarly, moving expensive I/O operations like `os.path.realpath` inside conditional blocks prevents redundant disk access when configuration (like path exclusions) isn't utilized. **Action:** When working on large text processing or disk operations, verify if early exit conditions or conditional execution can bypass the expensive system or library calls. +## 2024-06-25 - [Optimize Set Intersections in Hot Loops] +**Learning:** Using the `&` operator for set intersections creates intermediate sets, leading to unnecessary memory allocations and performance degradation inside hot loops. +**Action:** Use the in-place `.intersection_update()` method when sequentially intersecting sets to mutate the existing set directly, avoiding intermediate allocations. diff --git a/transcript_search.py b/transcript_search.py index 1660c1f2..3f1e1333 100644 --- a/transcript_search.py +++ b/transcript_search.py @@ -241,9 +241,11 @@ def search(self, query: str) -> list[Match]: postings = self._postings.get(term) if not postings: return [] - candidates = ( - set(postings) if candidates is None else candidates & postings - ) + if candidates is None: + candidates = set(postings) + else: + # ⚡ Bolt: Use .intersection_update() for in-place set intersection to avoid intermediate memory allocations and improve execution speed. + candidates.intersection_update(postings) if not candidates: return []