⚡️ Speed up method JavaAssertTransformer._extract_target_calls by 1,498% in PR #1199 (omni-java)
#1359
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.