⚡️ Speed up function transform_java_assertions by 19% in PR #1199 (omni-java)#1365
Open
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Open
⚡️ Speed up function transform_java_assertions by 19% in PR #1199 (omni-java)#1365codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
transform_java_assertions by 19% in PR #1199 (omni-java)#1365codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Conversation
This optimization achieves an **18% runtime improvement** by targeting two key bottlenecks identified through line profiling: ## Primary Optimization: Nested Assertion Filtering (16.2% → 2.9% of runtime) The original code used a **quadratic O(n²) double loop** to detect nested assertions, spending 16.2% of total time in the inner loop checking every assertion against every other assertion. The optimized version extracts this logic into `_exclude_nested()` which: 1. **Groups assertions by start position** to avoid redundant comparisons 2. **Uses prefix maximum tracking** to detect containment by earlier-starting assertions in O(1) per element 3. **Processes same-start groups efficiently** using end position counts only when needed For test cases with many assertions (like `test_many_assertions_with_target_calls` with 100 assertions), this reduces ~10,000 comparisons to ~100 operations, achieving **27% faster runtime** on that workload. ## Secondary Optimization: String Replacement Strategy (0.6% → 0.3% of runtime) The original code applied replacements in **reverse order using repeated string slicing** (`result[:start] + replacement + result[end:]`), creating a new string copy for each replacement. The optimized version: 1. **Builds the result in a single forward pass** using list parts 2. **Appends unchanged segments and replacements** to avoid intermediate string copies 3. **Joins all parts once** at the end with `"".join(result_parts)` This is particularly effective for large source files with many assertions (e.g., `test_performance_with_large_source`), showing **33% faster runtime**. ## Impact Based on Function References The function references show `transform_java_assertions` is called extensively in test transformation workflows, processing assertion-heavy test files. The optimization particularly benefits: - **Large test suites** with many assertions per method (common in parameterized tests) - **Nested test structures** (assertAll blocks) where the nested filtering is critical - **Test files with 100+ assertions** where both optimizations compound their benefits The changes are **purely algorithmic improvements** with no behavior modifications—all test cases show identical correctness, just faster execution.
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.📄 19% (0.19x) speedup for
transform_java_assertionsincodeflash/languages/java/remove_asserts.py⏱️ Runtime :
27.3 milliseconds→23.0 milliseconds(best of91runs)📝 Explanation and details
This optimization achieves an 18% runtime improvement by targeting two key bottlenecks identified through line profiling:
Primary Optimization: Nested Assertion Filtering (16.2% → 2.9% of runtime)
The original code used a quadratic O(n²) double loop to detect nested assertions, spending 16.2% of total time in the inner loop checking every assertion against every other assertion. The optimized version extracts this logic into
_exclude_nested()which:For test cases with many assertions (like
test_many_assertions_with_target_callswith 100 assertions), this reduces ~10,000 comparisons to ~100 operations, achieving 27% faster runtime on that workload.Secondary Optimization: String Replacement Strategy (0.6% → 0.3% of runtime)
The original code applied replacements in reverse order using repeated string slicing (
result[:start] + replacement + result[end:]), creating a new string copy for each replacement. The optimized version:"".join(result_parts)This is particularly effective for large source files with many assertions (e.g.,
test_performance_with_large_source), showing 33% faster runtime.Impact Based on Function References
The function references show
transform_java_assertionsis called extensively in test transformation workflows, processing assertion-heavy test files. The optimization particularly benefits:The changes are purely algorithmic improvements with no behavior modifications—all test cases show identical correctness, just faster execution.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-04T02.05.19and push.