⚡️ Speed up function _extract_exception_from_message by 92% in PR #1120 (comparator-wrapped-exceptions)
#1122
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 #1120
If you approve this dependent PR, these changes will be merged into the original PR branch
comparator-wrapped-exceptions.📄 92% (0.92x) speedup for
_extract_exception_from_messageincodeflash/verification/comparator.py⏱️ Runtime :
1.99 milliseconds→1.04 milliseconds(best of208runs)📝 Explanation and details
The optimized code achieves a 92% speedup through two key optimizations:
1. Precompiled Regex Pattern (Primary Optimization)
The original code compiled the regex pattern
r"got (\w+)\(['\"]"on every function call usingre.search(). The optimized version precompiles this pattern once at module load time as_RE_GOT_EXC = re.compile(r"got (\w+)\(['\"]").Why this matters: Regex compilation involves parsing the pattern string, building an internal state machine, and allocating memory structures. Line profiler shows the original
re.search()call taking 4.39ms (48.7% of total time), while the optimized precompiled search takes only 1.25ms (23.8%) - a 71% reduction in just this line.2. Module-level Import
Moving
import builtinsfrom inside the function to module scope eliminates repeated import lookups. While Python caches imports insys.modules, the original code still incurred overhead finding and loading the cached module on each call (1.36ms, 15.1% of runtime). The optimized version eliminates this entirely.Performance Impact by Test Case Type
The annotated tests show the optimization is most effective for:
ValueError,TypeError) see 74-90% speedupCall Context Impact
Based on
function_references, this function is called from_get_wrapped_exception()which handles exception unwrapping. Since exception handling can occur in hot paths (loops, recursive calls, error recovery flows), and the function may be called multiple times per exception chain, the optimization compounds significantly in real-world usage where hundreds or thousands of exceptions might be processed.The optimization is particularly valuable when processing many exceptions (as shown in bulk test cases improving by 81-104%), which aligns with typical usage in verification/comparison workflows where exception matching happens repeatedly.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
🔎 Click to see Concolic Coverage Tests
codeflash_concolic_hf5mz_7s/tmpfrmfcdtv/test_concolic_coverage.py::test__extract_exception_from_messageTo edit these changes
git checkout codeflash/optimize-pr1120-2026-01-20T06.21.39and push.