⚡️ Speed up function re_extract_from_cache by 18,223% in PR #1852 (cf-1846-port-perf-improvements)#1853
Conversation
The optimization added early-exit logic to `add_needed_imports_from_module` that checks whether the source module contains any module-level imports before invoking the heavyweight `GatherImportsVisitor` and downstream import-merging machinery. In the common case where a pruned module has no imports (or only nested ones inside functions), line profiling showed the gatherer and two `AddImportsVisitor`/`RemoveImportsVisitor` transforms consumed 36% of original runtime; the early exit skips all three, falling back to the destination code immediately. A second early exit after gathering verifies the visitor actually collected imports, avoiding redundant CST transformations when the source is import-free. Combined, these checks eliminate ~99% of the work when imports are absent, yielding an 18222% speedup with no semantic change because the fallback path always returned the correct destination code.
|
Claude finished @codeflash-ai[bot]'s task in 5s —— View job PR Review SummaryPrek Checks✅ Code Review🔴 Critical Bug:
|
e820e11
into
cf-1846-port-perf-improvements
⚡️ This pull request contains optimizations for PR #1852
If you approve this dependent PR, these changes will be merged into the original PR branch
cf-1846-port-perf-improvements.📄 18,223% (182.23x) speedup for
re_extract_from_cacheincodeflash/languages/python/context/code_context_extractor.py⏱️ Runtime :
634 milliseconds→3.46 milliseconds(best of250runs)📝 Explanation and details
The optimization added early-exit logic to
add_needed_imports_from_modulethat checks whether the source module contains any module-level imports before invoking the heavyweightGatherImportsVisitorand downstream import-merging machinery. In the common case where a pruned module has no imports (or only nested ones inside functions), line profiling showed the gatherer and twoAddImportsVisitor/RemoveImportsVisitortransforms consumed 36% of original runtime; the early exit skips all three, falling back to the destination code immediately. A second early exit after gathering verifies the visitor actually collected imports, avoiding redundant CST transformations when the source is import-free. Combined, these checks eliminate ~99% of the work when imports are absent, yielding an 18222% speedup with no semantic change because the fallback path always returned the correct destination code.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1852-2026-03-17T06.54.17and push.