⚡️ Speed up method JavaAssertTransformer._extract_target_calls by 1,498% in PR #1199 (omni-java)#1359
Open
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Open
Conversation
This optimization achieves a dramatic **1497% speedup** (from 107ms to 6.71ms) by eliminating expensive regex operations and repeated string slicing in hot parsing paths. **Key optimizations:** 1. **Regex pattern caching**: The compiled regex pattern for matching method names is now cached in `__init__` as `self._method_pattern` instead of being recompiled on every `_extract_target_calls` call. This alone saves ~7ms per call based on the profiler data (2.2% of original runtime). 2. **Index-based receiver detection**: Replaced two expensive `re.search()` calls (consuming 31.8% of original runtime) with character-by-character backwards scanning using string methods like `isspace()`, `isalpha()`, and `isalnum()`. This avoids: - Creating temporary string slices (`content[:method_start]`, `before_method.rstrip()`) - Regex compilation and matching overhead - The complex pattern matching for identifiers and "new ClassName" detection 3. **Optimized parenthesis matching**: In `_find_balanced_parens`, eliminated the repeated `code[pos-1]` lookup (consuming ~13.9% of runtime) by maintaining a rolling `prev_char` variable that's updated once per iteration. Also cached `len(code)` in variable `n` to avoid repeated calls. **Why this is faster:** - **Reduced allocations**: Index-based scanning avoids creating intermediate string slices that need allocation and garbage collection - **Better cache locality**: Character-by-character scanning with indices keeps data in CPU cache, whereas regex engines perform more complex state transitions - **Eliminated regex overhead**: Python's `re` module has significant overhead for pattern compilation and matching that pure Python string operations avoid **Test case performance:** The optimization shows consistent improvements across all test cases: - Simple cases: 40-70% faster (e.g., basic receiver detection) - Large-scale tests: Up to **11,000% faster** (test with 150 consecutive method calls improved from 36.5ms to 328μs) - Edge cases with complex nesting: 50-65% faster The most dramatic gains occur in tests with many method calls (like `test_many_consecutive_method_calls_on_same_object`) because the regex pattern caching and index-based scanning compound their benefits when processing multiple matches in the same content string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #1199
If you approve this dependent PR, these changes will be merged into the original PR branch
omni-java.📄 1,498% (14.98x) speedup for
JavaAssertTransformer._extract_target_callsincodeflash/languages/java/remove_asserts.py⏱️ Runtime :
107 milliseconds→6.71 milliseconds(best of156runs)📝 Explanation and details
This optimization achieves a dramatic 1497% speedup (from 107ms to 6.71ms) by eliminating expensive regex operations and repeated string slicing in hot parsing paths.
Key optimizations:
Regex pattern caching: The compiled regex pattern for matching method names is now cached in
__init__asself._method_patterninstead of being recompiled on every_extract_target_callscall. This alone saves ~7ms per call based on the profiler data (2.2% of original runtime).Index-based receiver detection: Replaced two expensive
re.search()calls (consuming 31.8% of original runtime) with character-by-character backwards scanning using string methods likeisspace(),isalpha(), andisalnum(). This avoids:content[:method_start],before_method.rstrip())Optimized parenthesis matching: In
_find_balanced_parens, eliminated the repeatedcode[pos-1]lookup (consuming ~13.9% of runtime) by maintaining a rollingprev_charvariable that's updated once per iteration. Also cachedlen(code)in variablento avoid repeated calls.Why this is faster:
remodule has significant overhead for pattern compilation and matching that pure Python string operations avoidTest case performance:
The optimization shows consistent improvements across all test cases:
The most dramatic gains occur in tests with many method calls (like
test_many_consecutive_method_calls_on_same_object) because the regex pattern caching and index-based scanning compound their benefits when processing multiple matches in the same content string.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-04T01.31.19and push.