⚡️ Speed up method JavaAssertTransformer._detect_variable_assignment by 33% in PR #1443 (fix/java-exception-assignment-instrumentation)#1444
Merged
mashraf-222 merged 1 commit intoFeb 11, 2026
Conversation
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:** 1. **Precompiled regex pattern**: The regex pattern `r"(\w+(?:<[^>]+>)?)\s+(\w+)\s*=\s*$"` is now compiled once in `__init__` and stored as `self._assign_re`, rather than being recompiled on every call to `_detect_variable_assignment`. 2. **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 using `self._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_start` and `assertion_start` as positional bounds to `search()`, we avoid creating the temporary `line_before_assert` substring (which took 5% of time in the original), reducing memory churn. **Performance Across Test Cases:** The optimization shows consistent improvements across all scenarios: - **Simple cases**: 35-45% faster (e.g., simple variable assignment: 39.1% faster) - **No-match cases**: 82-101% faster (e.g., no assignment: 101% faster) - regex compilation was pure overhead here - **Complex generics**: Still 6-14% faster despite more complex matching - **Large-scale test** (1000 iterations): 36.7% faster, proving the benefit scales with repeated calls **Impact on Workloads:** Since `_detect_variable_assignment` is called for every assertion in Java test code being analyzed, and the `JavaAssertTransformer` is 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).
5c302bf
into
fix/java-exception-assignment-instrumentation
12 of 28 checks passed
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 #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.