⚡️ Speed up function fix_imports_inside_blocks by 171% in PR #1901 (cf-fix-imports-inside-blocks)#1903
Conversation
The optimization consolidated three separate regex substitutions into a single combined pattern with a callback function, reducing the number of full-string scans from three to one. The original code applied `_INDENTED_DEFAULT_IMPORT_RE.sub()`, then `_INDENTED_NAMED_IMPORT_RE.sub()`, then `_INDENTED_NAMESPACE_IMPORT_RE.sub()` in sequence, each traversing the entire input string. The optimized version uses one alternation-based regex (`_COMBINED_IMPORT_RE`) that matches all three import types in a single pass, delegating replacement logic to `_replace_import()` which branches on which capture groups are populated. Line profiler shows the hot loop (the three consecutive `.sub()` calls accounting for 100% of original runtime) is now a single `.sub()` call, cutting per-iteration cost from ~81 µs to ~28 µs across typical inputs. No correctness or metric regressions observed.
|
Claude finished @codeflash-ai[bot]'s task in 3m 18s —— View job PR Review Summary
Prek ChecksPrek passed. Mypy found 3 errors (2 × Code ReviewBug fixed (auto-corrected): The optimization PR introduced Optimization correctness: The consolidation of three sequential regex passes ( No other issues — no security concerns, no breaking API changes, no accidental file inclusions. Duplicate DetectionNo duplicates detected. The new Other Open Optimization PRs
|
7b12f5f
into
cf-fix-imports-inside-blocks
The optimization PR #1903 replaced the three individual regex patterns with _COMBINED_IMPORT_RE, but left the originals in place. Remove the dead code. Co-authored-by: mohammed ahmed <undefined@users.noreply.github.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
⚡️ This pull request contains optimizations for PR #1901
If you approve this dependent PR, these changes will be merged into the original PR branch
cf-fix-imports-inside-blocks.📄 171% (1.71x) speedup for
fix_imports_inside_blocksincodeflash/languages/javascript/edit_tests.py⏱️ Runtime :
3.59 milliseconds→1.32 milliseconds(best of88runs)📝 Explanation and details
The optimization consolidated three separate regex substitutions into a single combined pattern with a callback function, reducing the number of full-string scans from three to one. The original code applied
_INDENTED_DEFAULT_IMPORT_RE.sub(), then_INDENTED_NAMED_IMPORT_RE.sub(), then_INDENTED_NAMESPACE_IMPORT_RE.sub()in sequence, each traversing the entire input string. The optimized version uses one alternation-based regex (_COMBINED_IMPORT_RE) that matches all three import types in a single pass, delegating replacement logic to_replace_import()which branches on which capture groups are populated. Line profiler shows the hot loop (the three consecutive.sub()calls accounting for 100% of original runtime) is now a single.sub()call, cutting per-iteration cost from ~81 µs to ~28 µs across typical inputs. No correctness or metric regressions observed.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_languages/test_javascript_support.py::TestFixImportsInsideBlocks.test_import_inside_describe_blocktest_languages/test_javascript_support.py::TestFixImportsInsideBlocks.test_import_inside_jest_mock_defaulttest_languages/test_javascript_support.py::TestFixImportsInsideBlocks.test_import_inside_jest_mock_namedtest_languages/test_javascript_support.py::TestFixImportsInsideBlocks.test_mixed_top_level_and_indentedtest_languages/test_javascript_support.py::TestFixImportsInsideBlocks.test_namespace_import_inside_blocktest_languages/test_javascript_support.py::TestFixImportsInsideBlocks.test_top_level_imports_preserved🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1901-2026-03-25T17.27.15and push.