⚡️ Speed up method JavaAssertTransformer._generate_exception_replacement by 14% in PR #1443 (fix/java-exception-assignment-instrumentation)
#1445
+2
−1
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 #1443
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/java-exception-assignment-instrumentation.📄 14% (0.14x) speedup for
JavaAssertTransformer._generate_exception_replacementincodeflash/languages/java/remove_asserts.py⏱️ Runtime :
2.31 milliseconds→2.04 milliseconds(best of190runs)📝 Explanation and details
The optimization achieves a 13% runtime improvement (2.31ms → 2.04ms) by replacing Python's
str.endswith()method call with a direct last-character index check (code_to_run[-1] != ";"instead ofnot code_to_run.endswith(";")).Key optimization:
The critical change occurs in the lambda body processing path, which is executed in 2,936 out of 3,943 invocations (74% of calls). By replacing the
endswith()method call with direct indexing, the code eliminates:endswithLine profiler data shows the optimized check (
if code_to_run and code_to_run[-1] != ";") runs in 964ns versus 1.24μs for the originalendswith()call—a 22% improvement on this single line that executes nearly 3,000 times per test run.Why this works:
In CPython, direct character indexing (
[-1]) is implemented as a simple array lookup in the string's internal buffer, whileendswith()involves:For a single-character comparison, the indexing approach is significantly faster.
Test results validation:
The annotated tests show consistent improvements across all test cases:
The optimization is particularly effective for workloads with many assertion transformations, as demonstrated by the large-scale tests (1000+ invocations) showing 17-18% improvements.
Impact:
Since
JavaAssertTransformeris used to process Java test code during optimization workflows, this change directly reduces the time to transform assertion-heavy test files. The function processes each assertion statement individually, so files with hundreds of assertions will see cumulative time savings proportional to the assertion count.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1443-2026-02-10T21.52.05and push.