Skip to content

⚡ Bolt: [performance improvement] Optimize solution lookups to O(N)#140

Open
glacy wants to merge 1 commit into
mainfrom
bolt-opt-O-N-loop-5598398590228265232
Open

⚡ Bolt: [performance improvement] Optimize solution lookups to O(N)#140
glacy wants to merge 1 commit into
mainfrom
bolt-opt-O-N-loop-5598398590228265232

Conversation

@glacy

@glacy glacy commented Jun 21, 2026

Copy link
Copy Markdown
Owner

💡 What: Refactored the $O(N \times M)$ nested loop used to find solutions for exercises into an $O(N)$ dictionary lookup strategy.

🎯 Why: To improve performance when processing large numbers of materials. Nested loops iterating over exercises and solutions can degrade performance unnecessarily (quadratic scaling). A dictionary lookup reduces the time complexity of the matching phase to $O(1)$ per exercise.

📊 Impact: Reduces time complexity of matching exercises with their solutions from $O(N \times M)$ to $O(N)$, saving processing time during material extraction and indexing.

🔬 Measurement: Extracting and indexing materials from a large directory structure will measure noticeably faster. Validated by ensuring the precomputed lookup logic accurately replicates the first-match functionality of the original nested loop's break statement.


PR created automatically by Jules for task 5598398590228265232 started by @glacy

- Replaces O(N*M) search logic for solutions in `get_all_exercises` (`evolutia/material_extractor.py`) and `index_materials` (`evolutia/rag/rag_indexer.py`).
- Precomputes a lookup dictionary in O(N) time for matching `exercise_label`.
- Preserves the original `break`'s first-match logic by verifying `if key not in dict` before inserting.
- Formatted modified files with black and ruff.

Co-authored-by: glacy <1131951+glacy@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings June 21, 2026 17:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes exercise→solution matching by replacing per-exercise nested scans of solutions with a precomputed per-material dictionary lookup, reducing matching from (O(N \times M)) to (O(N)) during extraction and RAG indexing.

Changes:

  • Precompute solutions_by_label dictionaries to do (O(1)) solution retrieval per exercise in both material extraction and RAG indexing.
  • Preserve original “first match wins” behavior by only setting a dict entry when the key is missing.
  • Apply broad formatting/quoting cleanup in the touched modules.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
evolutia/rag/rag_indexer.py Uses a per-material solutions_by_label dict for faster solution lookup during indexing; includes refactors around embedding/chunk handling and formatting.
evolutia/material_extractor.py Uses a per-material solutions_by_label dict for faster solution lookup when aggregating exercises; includes formatting cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +197 to +200
elif self.embedding_provider == "sentence-transformers":
return self.embedding_model.encode(
texts, show_progress_bar=True, batch_size=32
).tolist()
documents = []
metadatas = []

for i, (chunk, embedding) in enumerate(zip(chunks, embeddings)):

source = metadata.get("source_file", "reading")

for i, (chunk, embedding) in enumerate(zip(chunks, embeddings)):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants