⚡️ Speed up method JavaAssertTransformer._find_junit_assertions by 22% in PR #1295 (feat/java-remove-asserts-transformer)#1327
Closed
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Conversation
The optimized code achieves a **22% runtime improvement** by eliminating repeated regex pattern compilation overhead in hot paths.
**Key Optimizations:**
1. **Pre-compiled Regex Patterns (Primary Speedup):**
- Moved three `re.compile()` calls from method bodies to `__init__`:
- `_junit_pattern`: Compiled once instead of on every `_find_junit_assertions()` call
- `_target_call_pattern`: Compiled once instead of on every `_extract_target_calls()` call
- `_lambda_pattern`: Compiled once instead of on every `_extract_lambda_body()` call
- The line profiler shows `_find_junit_assertions()` dropped from 78.8ms to 63.8ms (19% faster), with regex compilation time eliminated from the 4.2% hotspot
- Similarly, `_extract_target_calls()` improved from 18.6ms to 12.1ms (35% faster), removing its 34.8% regex compilation overhead
2. **Optimized String Indexing in `_find_balanced_parens()`:**
- Caches `len(code)` as `code_len` to avoid repeated function calls in the tight loop
- Restructured escape sequence checking to avoid redundant `code[pos - 1]` lookups
- Reduced per-character overhead in the parser, improving from 26.7ms to 23.4ms (12% faster)
3. **Simplified Lambda Body Extraction:**
- Replaced nested `content[body_start:].index("{")` calls with single `content.index("{", body_start)`
- Reduced method time from 456μs to 144μs (68% faster on assertThrows cases)
**Why This Works:**
- Regex compilation in Python is expensive (involves pattern parsing, DFA construction). By compiling patterns once during initialization rather than on every method call, we eliminate this overhead from the critical path
- The test results show consistent 30-70% improvements across all test cases, with the largest gains on simpler assertions where regex compilation dominated runtime
- These optimizations are particularly effective for code analysis tools that process many assertions repeatedly (see the `test_large_scale_many_assertions_under_limit` improving from 1.54ms to 1.13ms)
The changes preserve all functionality while significantly improving performance for Java assertion analysis workflows.
3 tasks
Base automatically changed from
feat/java-remove-asserts-transformer
to
omni-java
February 3, 2026 22:18
Collaborator
|
Closing stale bot PR. |
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 #1295
If you approve this dependent PR, these changes will be merged into the original PR branch
feat/java-remove-asserts-transformer.📄 22% (0.22x) speedup for
JavaAssertTransformer._find_junit_assertionsincodeflash/languages/java/remove_asserts.py⏱️ Runtime :
9.88 milliseconds→8.08 milliseconds(best of100runs)📝 Explanation and details
The optimized code achieves a 22% runtime improvement by eliminating repeated regex pattern compilation overhead in hot paths.
Key Optimizations:
Pre-compiled Regex Patterns (Primary Speedup):
re.compile()calls from method bodies to__init__:_junit_pattern: Compiled once instead of on every_find_junit_assertions()call_target_call_pattern: Compiled once instead of on every_extract_target_calls()call_lambda_pattern: Compiled once instead of on every_extract_lambda_body()call_find_junit_assertions()dropped from 78.8ms to 63.8ms (19% faster), with regex compilation time eliminated from the 4.2% hotspot_extract_target_calls()improved from 18.6ms to 12.1ms (35% faster), removing its 34.8% regex compilation overheadOptimized String Indexing in
_find_balanced_parens():len(code)ascode_lento avoid repeated function calls in the tight loopcode[pos - 1]lookupsSimplified Lambda Body Extraction:
content[body_start:].index("{")calls with singlecontent.index("{", body_start)Why This Works:
test_large_scale_many_assertions_under_limitimproving from 1.54ms to 1.13ms)The changes preserve all functionality while significantly improving performance for Java assertion analysis workflows.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1295-2026-02-03T21.43.34and push.