-
Notifications
You must be signed in to change notification settings - Fork 35
β‘ Bolt: [performance improvement] Optimize RAG retrieval path #709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -85,3 +85,11 @@ | |||||||||||||||||||||
| ## 2026-05-16 - Pre-processing for RAG Retrieval | ||||||||||||||||||||||
| **Learning:** In RAG (Retrieval-Augmented Generation) systems with static or semi-static policy datasets, performing tokenization, regex substitution, and string formatting inside the retrieval loop is a significant bottleneck that scales with the number of policies. | ||||||||||||||||||||||
| **Action:** Move all deterministic operations (tokenization, formatting, regex matching prep) to a one-time initialization step to ensure the retrieval hot-path only performs necessary set intersections and similarity calculations. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ## 2025-05-18 - Optimized Jaccard Similarity for RAG | ||||||||||||||||||||||
| **Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with for early exits significantly reduces CPU cycles for non-matching documents. | ||||||||||||||||||||||
| **Action:** Use mathematical union length and for set similarity comparisons in high-frequency retrieval paths. | ||||||||||||||||||||||
|
Comment on lines
+90
to
+91
|
||||||||||||||||||||||
| **Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with for early exits significantly reduces CPU cycles for non-matching documents. | |
| **Action:** Use mathematical union length and for set similarity comparisons in high-frequency retrieval paths. | |
| **Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with `isdisjoint()` for early exits significantly reduces CPU cycles for non-matching documents. | |
| **Action:** Use mathematical union length and `isdisjoint()` for set similarity comparisons in high-frequency retrieval paths. |
Copilot
AI
Apr 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section is duplicated (two identical "## 2025-05-18 - Optimized Jaccard Similarity for RAG" entries). Please remove one to avoid conflicting guidance / unnecessary repetition in the Bolt notes.
| **Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with for early exits significantly reduces CPU cycles for non-matching documents. | |
| **Action:** Use mathematical union length and for set similarity comparisons in high-frequency retrieval paths. | |
| ## 2025-05-18 - Optimized Jaccard Similarity for RAG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the duplicate/broken RAG optimization entry.
Line 89 duplicates the heading from Line 93, and the first copy has incomplete inline references (with ... / and ...). Keep only one corrected section to avoid MD024 and unclear guidance.
π§Ή Suggested cleanup
-## 2025-05-18 - Optimized Jaccard Similarity for RAG
-**Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with for early exits significantly reduces CPU cycles for non-matching documents.
-**Action:** Use mathematical union length and for set similarity comparisons in high-frequency retrieval paths.
-
## 2025-05-18 - Optimized Jaccard Similarity for RAG
**Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with `isdisjoint()` for early exits significantly reduces CPU cycles for non-matching documents.
**Action:** Use mathematical union length and `isdisjoint()` for set similarity comparisons in high-frequency retrieval paths.π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ## 2025-05-18 - Optimized Jaccard Similarity for RAG | |
| **Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with for early exits significantly reduces CPU cycles for non-matching documents. | |
| **Action:** Use mathematical union length and for set similarity comparisons in high-frequency retrieval paths. | |
| ## 2025-05-18 - Optimized Jaccard Similarity for RAG | |
| **Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with `isdisjoint()` for early exits significantly reduces CPU cycles for non-matching documents. | |
| **Action:** Use mathematical union length and `isdisjoint()` for set similarity comparisons in high-frequency retrieval paths. | |
| ## 2025-05-18 - Optimized Jaccard Similarity for RAG | |
| **Learning:** Calculating Jaccard similarity in a hot loop can be optimized by using the inclusion-exclusion principle (|A βͺ B| = |A| + |B| - |A β© B|) to avoid the overhead of set union construction. Combining this with `isdisjoint()` for early exits significantly reduces CPU cycles for non-matching documents. | |
| **Action:** Use mathematical union length and `isdisjoint()` for set similarity comparisons in high-frequency retrieval paths. |
π§° Tools
πͺ markdownlint-cli2 (0.22.1)
[warning] 93-93: Multiple headings with the same content
(MD024, no-duplicate-heading)
π€ Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In @.jules/bolt.md around lines 89 - 95, Remove the duplicate/broken "Optimized
Jaccard Similarity for RAG" entry and keep the corrected version: locate the two
identical headings "Optimized Jaccard Similarity for RAG", delete the first
block that contains incomplete inline references ("with ..." / "and ..."), and
ensure only the second, corrected paragraph (mentioning inclusion-exclusion
principle and `isdisjoint()` with the action to use mathematical union length
and `isdisjoint()`) remains as the single entry.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -46,10 +46,12 @@ def _prepare_policies(self): | |||||||
| source = policy.get('source', 'Unknown') | ||||||||
|
|
||||||||
| content = f"{title} {text}" | ||||||||
| content_tokens = self._tokenize(content) | ||||||||
|
|
||||||||
| self._prepared_policies.append({ | ||||||||
| 'title_tokens': self._tokenize(title), | ||||||||
| 'content_tokens': self._tokenize(content), | ||||||||
| 'content_tokens': content_tokens, | ||||||||
| 'content_tokens_len': len(content_tokens), | ||||||||
| 'formatted': f"**{title}**: {text} (Source: {source})", | ||||||||
| 'original': policy | ||||||||
| }) | ||||||||
|
|
@@ -73,30 +75,33 @@ def retrieve(self, query: str, threshold: float = 0.05) -> Optional[str]: | |||||||
| if not query_tokens: | ||||||||
| return None | ||||||||
|
|
||||||||
| query_tokens_len = len(query_tokens) | ||||||||
| best_score = 0.0 | ||||||||
| best_formatted = None | ||||||||
|
|
||||||||
| for prepared in self._prepared_policies: | ||||||||
| policy_tokens = prepared['content_tokens'] | ||||||||
|
|
||||||||
| if not policy_tokens: | ||||||||
| # Optimization: Use isdisjoint() for fast early exit | ||||||||
| if query_tokens.isdisjoint(policy_tokens): | ||||||||
| continue | ||||||||
|
|
||||||||
| # Jaccard Similarity | ||||||||
| # Optimization: Mathematical union length |A union B| = |A| + |B| - |A intersection B| | ||||||||
| # This avoids the overhead of building a new set with .union() | ||||||||
| intersection = query_tokens.intersection(policy_tokens) | ||||||||
| # Use pre-calculated set for union if possible? | ||||||||
| # Union depends on query_tokens, so must be calculated. | ||||||||
| union = query_tokens.union(policy_tokens) | ||||||||
| intersection_len = len(intersection) | ||||||||
|
|
||||||||
| if not union: | ||||||||
| union_len = query_tokens_len + prepared['content_tokens_len'] - intersection_len | ||||||||
|
|
||||||||
| if union_len == 0: | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: This Prompt for AI agents |
||||||||
| continue | ||||||||
|
|
||||||||
|
Comment on lines
+96
to
99
|
||||||||
| if union_len == 0: | |
| continue |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: Duplicate section: there are two identical
## 2025-05-18 - Optimized Jaccard Similarity for RAGheadings. This first copy also has missing inline code (Combining this with for early exitsβ should beisdisjoint()). Remove this broken duplicate and keep the complete entry below.Prompt for AI agents