Skip to content

⚡ Optimize regex performance in CompatibilityLayer#505

Open
yaskhan wants to merge 1 commit intomainfrom
performance-regex-optimization-compatibility-layer-3851134103757350284
Open

⚡ Optimize regex performance in CompatibilityLayer#505
yaskhan wants to merge 1 commit intomainfrom
performance-regex-optimization-compatibility-layer-3851134103757350284

Conversation

@yaskhan
Copy link
Copy Markdown
Owner

@yaskhan yaskhan commented Mar 24, 2026

💡 What:

Implemented pre-compilation of regular expressions in the CompatibilityLayer class within py2v_transpiler/core/compatibility.py.

🎯 Why:

The previous implementation used re.sub() and re.match() with raw string patterns inside methods that are called repeatedly during source pre-processing. This caused Python to either recompile the patterns or perform a cache lookup on every call. By moving these to class-level pre-compiled objects, we avoid this overhead.

📊 Measured Improvement:

Using a focused micro-benchmark for the affected methods, the pre-compiled approach showed a 22.36% performance improvement in total execution time compared to the uncompiled approach when processing a source with 100 repetitions of various syntax patterns over 10,000 iterations.

Version Total Time (10k iterations) Avg Time/Iteration
Uncompiled 36.0287s 0.003603s
Compiled 27.9717s 0.002797s

All existing tests in py2v_transpiler/tests/test_compatibility.py and py2v_transpiler/tests/test_tstrings.py passed successfully.


PR created automatically by Jules for task 3851134103757350284 started by @yaskhan

Move frequently used regular expressions to class-level constants to
avoid recompilation overhead during source pre-processing.

This change impacts:
- `_preprocess_tstrings`
- `_preprocess_generic_match`
- `_preprocess_bracketless_except`

Measured improvement in regex-intensive micro-benchmarks shows a
reduction in execution time for these patterns by approximately 22%.

Co-authored-by: yaskhan <3676373+yaskhan@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.

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