⚡️ Speed up function transform_java_assertions by 19% in PR #1199 (omni-java)
#1365
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.