⚡️ Speed up method JavaAssertTransformer._detect_variable_assignment by 33% in PR #1443 (fix/java-exception-assignment-instrumentation)
#1444
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.📄 33% (0.33x) speedup for
JavaAssertTransformer._detect_variable_assignmentincodeflash/languages/java/remove_asserts.py⏱️ Runtime :
1.63 milliseconds→1.23 milliseconds(best of250runs)📝 Explanation and details
The optimization achieves a 33% runtime speedup (from 1.63ms to 1.23ms) by eliminating repeated regex compilation overhead through two key changes:
What Changed:
Precompiled regex pattern: The regex pattern
r"(\w+(?:<[^>]+>)?)\s+(\w+)\s*=\s*$"is now compiled once in__init__and stored asself._assign_re, rather than being recompiled on every call to_detect_variable_assignment.Direct substring search: Instead of first extracting
line_before_assert = source[line_start:assertion_start]and then searching it, the optimized version directly searches the source string usingself._assign_re.search(source, line_start, assertion_start)with positional parameters.Why This Is Faster:
Regex compilation overhead eliminated: Line profiler shows the original code spent 53.4% of total time (3.89ms out of 7.29ms) on
re.search(pattern, line_before_assert). This line was called 1,057 times, meaning the regex pattern was compiled 1,057 times. The optimized version reduces this to just 30.8% (1.20ms out of 3.91ms) by using a precompiled pattern.Reduced string allocations: By passing
line_startandassertion_startas positional bounds tosearch(), we avoid creating the temporaryline_before_assertsubstring (which took 5% of time in the original), reducing memory churn.Performance Across Test Cases:
The optimization shows consistent improvements across all scenarios:
Impact on Workloads:
Since
_detect_variable_assignmentis called for every assertion in Java test code being analyzed, and theJavaAssertTransformeris likely instantiated once per file/session, this optimization provides cumulative benefits. The precompilation happens once at instantiation, then every subsequent call benefits from the compiled pattern - making it especially valuable when processing files with many assertions (as demonstrated by the 1000-iteration test showing consistent 36.7% improvement).✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1443-2026-02-10T21.31.59and push.