Skip to content

⚡ Bolt: [performance improvement] Optimize MaterialExtractor iteration loops#94

Open
glacy wants to merge 1 commit into
mainfrom
bolt-optimize-material-extractor-4314469170521182925
Open

⚡ Bolt: [performance improvement] Optimize MaterialExtractor iteration loops#94
glacy wants to merge 1 commit into
mainfrom
bolt-optimize-material-extractor-4314469170521182925

Conversation

@glacy
Copy link
Copy Markdown
Owner

@glacy glacy commented May 5, 2026

💡 What:

  • In get_all_exercises (evolutia/material_extractor.py), replaced the $O(N \times M)$ nested loop that matches exercises to solutions with an $O(N)$ pre-computed dictionary lookup (solution_lookup), carefully preserving the original 'first match' fallback logic.
  • In extract_by_topic (evolutia/material_extractor.py), hoisted topic.lower() outside of the filesystem iteration loops to avoid redundant string allocations.
  • Fixed a bug in extract_from_file where an F821 Undefined name 'result' linting error prevented caching on successful extraction.
  • Adjusted unused variable logic on try block caching (cache_entry -> _).

🎯 Why:

  • The nested loop for resolving exercises against solutions scales poorly when parsing large repositories with hundreds of exercises. Hash map lookups provide $O(1)$ constant time access.
  • Calling .lower() redundantly inside .glob and for loops across file trees incurs unnecessary overhead.

📊 Impact:

  • Resolving solutions per exercise is strictly $O(N)$ instead of $O(N^2)$.
  • Reduces unnecessary object creation in hot path parsing strings.

🔬 Measurement:

  • Run python -m pytest tests/test_exercise_analyzer_cache.py or python -m pytest tests/ to confirm that baseline behavior remains identical.

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

- Replaced O(N*M) nested loop in `get_all_exercises` with an O(N) hash map lookup, preserving original first-match behavior.
- Hoisted redundant `topic.lower()` calls outside of the directory scanning loops in `extract_by_topic` to avoid repeated string allocations.

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 May 5, 2026 19:18
Copy link
Copy Markdown

Copilot AI left a comment

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 improves the performance of material parsing by optimizing solution lookup during exercise aggregation and reducing repeated string work in topic-based extraction. It also adjusts extract_from_file so successful results can be cached correctly.

Changes:

  • Optimizes get_all_exercises() by replacing the nested exercise→solution scan with a per-material dictionary lookup while preserving “first match wins” behavior.
  • Hoists topic.lower() in extract_by_topic() to avoid redundant lowercasing during directory iteration.
  • Fixes result caching in extract_from_file() and does some small refactors/formatting updates.

Reviewed changes

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

File Description
evolutia/material_extractor.py Performance improvements for solution resolution + minor refactors in caching and topic extraction paths.
evolutia.egg-info/SOURCES.txt Adds tests/test_markdown_parser.py to the generated sources list.
Comments suppressed due to low confidence (1)

evolutia/material_extractor.py:335

  • La validez del caché se decide comparando file_mtime contra _last_scan_timestamp, pero nunca se usa el timestamp almacenado en self._file_cache[file_path] ni el TTL (self._cache_ttl). Esto puede dejar el caché incorrecto/inefectivo (p.ej., TTL anunciado pero no aplicado). Sería más robusto comparar contra el mtime/timestamp guardado para ese archivo y/o invalidar por antigüedad usando _cache_ttl.
            file_mtime = file_path.stat().st_mtime

            # Usar el timestamp de escaneo más reciente para verificar
            if file_mtime > self._last_scan_timestamp:
                return False

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

Comment on lines +123 to +127
"data": result,
"timestamp": file_path.stat().st_mtime,
}
self._last_scan_timestamp = max(self._last_scan_timestamp, file_path.stat().st_mtime)
self._last_scan_timestamp = max(
self._last_scan_timestamp, file_path.stat().st_mtime
Comment on lines 329 to 331
try:
cache_entry = self._file_cache[file_path]
_ = self._file_cache[file_path]
file_mtime = file_path.stat().st_mtime
tests/test_exercise_analyzer.py
tests/test_exercise_analyzer_cache.py
tests/test_json_robustness.py
tests/test_markdown_parser.py
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