Skip to content

⚡ Bolt: Optimize math operations counting in math_extractor#17

Open
glacy wants to merge 1 commit intomainfrom
bolt/optimize-math-extractor-12306371029725047450
Open

⚡ Bolt: Optimize math operations counting in math_extractor#17
glacy wants to merge 1 commit intomainfrom
bolt/optimize-math-extractor-12306371029725047450

Conversation

@glacy
Copy link
Owner

@glacy glacy commented Feb 14, 2026

💡 What: Replaced 6 separate regex passes in count_math_operations with a single pass using a combined regex pattern with named groups.
🎯 Why: count_math_operations is called in a loop (via estimate_complexity) on potentially large strings. Reducing the number of passes from 6 to 1 improves performance from O(K*N) to O(N).
📊 Impact: Reduces regex overhead by ~83% (6 passes -> 1 pass).
🔬 Measurement: Verified with tests/test_math_extractor.py, ensuring no regressions and correct counting of edge cases.


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

Co-authored-by: glacy <1131951+glacy@users.noreply.github.com>
@google-labs-jules
Copy link
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 February 14, 2026 18:09
Copy link

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 optimizes the count_math_operations function in the math_extractor module by consolidating six separate regex patterns into a single combined pattern with named groups. This reduces the computational complexity from O(K*N) to O(N) where K=6 is the number of patterns and N is the string length.

Changes:

  • Replaced six individual pre-compiled regex patterns (INTEGRALS_PATTERN, DERIVATIVES_PATTERN, etc.) with one COMBINED_OPERATIONS_PATTERN using named groups
  • Refactored count_math_operations to use a single finditer loop instead of six separate findall calls
  • Added comprehensive edge case tests to validate the refactored implementation

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
evolutia/utils/math_extractor.py Replaced six separate regex patterns with one combined pattern using named groups; refactored count_math_operations to iterate once instead of six times
tests/test_math_extractor.py Added test_count_math_operations_edge_cases to validate correct behavior with overlapping patterns, multiple occurrences, and edge cases

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

VECTORS_PATTERN = re.compile(r'\\vec|\\mathbf')
MATRICES_PATTERN = re.compile(r'\\begin\{matrix\}|\\begin\{pmatrix\}|\\begin\{bmatrix\}')
FUNCTIONS_PATTERN = re.compile(r'\\sin|\\cos|\\tan|\\exp|\\log|\\ln')
# Combined pattern for math operations
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

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

This comment should be in Spanish to maintain consistency with the codebase convention. The removed comment "Patrones compilados para operaciones matemáticas" was in Spanish. Consider changing to "Patrón combinado para operaciones matemáticas" or similar.

Suggested change
# Combined pattern for math operations
# Patrón combinado para operaciones matemáticas

Copilot uses AI. Check for mistakes.
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.

1 participant