⚡ Bolt: Replace O(N*M) lookup with O(N) dict in exercise extraction#145
⚡ Bolt: Replace O(N*M) lookup with O(N) dict in exercise extraction#145glacy wants to merge 1 commit into
Conversation
Co-authored-by: glacy <1131951+glacy@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR improves performance during exercise extraction and RAG indexing by replacing per-exercise nested-loop solution matching with a precomputed dict lookup, reducing quadratic behavior when processing large material sets.
Changes:
- Precompute
solution_lookupdictionaries to match solutions to exercises in O(N+M) time. - Update
MaterialExtractor.get_all_exercisesandRAGIndexer.index_materialsto use the lookup while preserving first-match semantics. - Includes broad formatting/quoting changes 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/material_extractor.py | Uses a solution-label dict lookup in get_all_exercises to avoid nested-loop matching (but currently introduces a cache regression in extract_from_file). |
| evolutia/rag/rag_indexer.py | Uses a solution-label dict lookup in index_materials to avoid nested-loop matching; retains existing indexing flow (but chunk/embedding alignment needs a small fix). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return { | ||
| 'file_path': file_path, | ||
| 'frontmatter': frontmatter, | ||
| 'exercises': exercises, | ||
| 'solutions': solutions, | ||
| 'content_body': content_body # Exponer contenido para indexación de lecturas | ||
| "file_path": file_path, | ||
| "frontmatter": frontmatter, | ||
| "exercises": exercises, | ||
| "solutions": solutions, | ||
| "content_body": content_body, # Exponer contenido para indexación de lecturas | ||
| } |
| # Generar embeddings | ||
| embeddings = self._generate_embeddings_batch(chunks) | ||
|
|
||
| # Sincronizar chunks con embeddings (por si se filtraron vacíos en _generate_embeddings_batch) | ||
| # Aunque aquí preferimos filtrar antes para mantener consistencia | ||
| valid_indices = [i for i, chunk in enumerate(chunks) if chunk and chunk.strip()] | ||
| chunks = [chunks[i] for i in valid_indices] | ||
|
|
||
| if not chunks: | ||
| logger.warning( | ||
| f"Ejercicio {exercise.get('label', 'unknown')} no tiene contenido válido para indexar" | ||
| ) | ||
| return [] |
| # Generar embeddings | ||
| embeddings = self._generate_embeddings_batch(chunks) | ||
|
|
||
| # Sincronizar chunks con embeddings | ||
| valid_indices = [i for i, chunk in enumerate(chunks) if chunk and chunk.strip()] | ||
| chunks = [chunks[i] for i in valid_indices] | ||
|
|
||
| if not chunks: | ||
| logger.warning( | ||
| f"Lectura {metadata.get('title', 'unknown')} no tiene contenido válido para indexar" | ||
| ) | ||
| return [] |
💡 What: Replaced O(NM) nested loop searches for exercise solutions with O(N) hash map lookups in
MaterialExtractor.get_all_exercisesandRAGIndexer.index_materials.🎯 Why: Iterating over all solutions for every exercise scales quadratically O(NM) which slows down extraction and indexing processing for large document sets.
📊 Impact: Reduces time complexity from O(N*M) to O(N+M) for solution matching during exercise extraction and RAG indexing, improving performance scaling linearly.
🔬 Measurement: Verify tests pass with
export PYTHONPATH=$PYTHONPATH:.:mocks && /home/jules/.local/bin/pytest tests/ -v.PR created automatically by Jules for task 5561584019336000832 started by @glacy