Skip to content

⚡ Bolt: [performance improvement] Refactor nested loops to O(N) lookups#152

Open
glacy wants to merge 1 commit into
mainfrom
bolt-optimize-on-m-loops-15247401720627197240
Open

⚡ Bolt: [performance improvement] Refactor nested loops to O(N) lookups#152
glacy wants to merge 1 commit into
mainfrom
bolt-optimize-on-m-loops-15247401720627197240

Conversation

@glacy

@glacy glacy commented Jul 6, 2026

Copy link
Copy Markdown
Owner

💡 What: Refactored O(NM) nested loops in material_extractor.py and rag_indexer.py into an O(N) dictionary lookup.
🎯 Why: Nested loops matching solutions to exercises degrade performance drastically as the number of exercises and solutions grows. Replacing them with a dictionary lookup ensures linear scaling and removes unnecessary computation.
📊 Impact: Reduces time complexity from O(N
M) to O(N+M) for exercise-solution matching, ensuring the application remains responsive as datasets grow.
🔬 Measurement: Verified by testing locally and passing the pre-commit review. Tested against test_markdown_parser.py, test_config_discovery.py, and test_exercise_analyzer_cache.py ensuring regressions were not introduced.


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

Replaces inefficient nested loops for matching exercises to solutions
in `material_extractor.py` and `rag_indexer.py` with an O(N) dictionary
lookup that preserves first-match behavior. This improves scaling for
large lists. Formatted modified files using 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 July 6, 2026 17:52

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 refactors exercise→solution matching in the extraction/indexing pipeline from nested loops (O(N*M)) to dictionary lookups (O(N+M)), improving scalability when materials contain many exercises/solutions.

Changes:

  • Refactored solution lookup in MaterialExtractor.get_all_exercises() to use a solutions_dict while preserving first-match semantics.
  • Refactored solution lookup in RAGIndexer.index_materials() similarly, avoiding repeated scans of solutions per exercise.
  • Applied formatting/consistency updates (quoting, wrapping, minor cleanups) in both 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 Builds a per-material solutions_dict for O(1) solution retrieval during indexing; includes minor refactors and logging/exception-path changes.
evolutia/material_extractor.py Builds a per-material solutions_dict for O(1) solution retrieval when flattening exercises; includes formatting and small cache-code cleanup.
Comments suppressed due to low confidence (1)

evolutia/rag/rag_indexer.py:193

  • logger.error(f"Batch problemático: {batch}") logs the full embedding input batch, which may include large/sensitive document contents and can flood logs. Prefer logging only metadata (e.g., batch size) and use non-f-string parameterized logging.
                except Exception as e:
                    logger.error(f"Error en OpenAI embeddings: {e}")
                    logger.error(f"Batch problemático: {batch}")
                    raise

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

"""Limpia la colección (útil para re-indexar)."""
collection_name = self.collection.name
self.client.delete_collection(name=collection_name)
_ = self.config.get("vector_store", {})
# Verificar si el archivo fue modificado
try:
cache_entry = self._file_cache[file_path]
_ = self._file_cache[file_path]
model=self.embedding_model_name, input=batch
)
embeddings.extend([item.embedding for item in response.data])
except Exception as e:
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