⚡️ Speed up function _extract_calling_function by 18% in PR #1256 (refactor/use-function-to-optimize-in-js) - #1274
Closed
codeflash-ai[bot] wants to merge 1 commit into
Closed
Conversation
The optimized code achieves an 18% runtime improvement by replacing the exhaustive AST traversal with a **pruned depth-first search (DFS)** that skips irrelevant subtrees.
**Key optimization: Spatial pruning in AST traversal**
The original code uses `ast.walk(tree)`, which visits *every* node in the AST regardless of whether it could possibly contain the target function. The optimized version implements a stack-based DFS that prunes entire subtrees when their line ranges don't contain `ref_line`:
```python
# Prune nodes that can't contain the reference line
if node_start is not None:
node_end = getattr(node, "end_lineno", node_start) or node_start
if not (node_start <= ref_line <= node_end):
continue # Skip this entire subtree
```
This spatial pruning is highly effective because:
- ASTs for typical Python files contain hundreds of nodes (as seen in the profiler: 781 nodes visited in the original)
- Most nodes fall outside the reference line's range
- The optimized version visits only ~336 nodes (57% reduction), as shown in the profiler's `while stack` iteration count
**Secondary optimization: Deferred string splitting**
The code also moves `source_code.splitlines()` to execute only after finding a matching function, avoiding unnecessary work when no match exists (13 of 50 test cases in the profiler).
**Performance characteristics based on test results:**
The optimization is most effective for:
- **Large files with many functions** (83.4% speedup for 100-function file, 52.7% speedup for mixed-content file)
- **Edge cases with extreme ref_line values** (43-46% speedup when `ref_line` is far outside valid ranges, enabling early pruning)
- **Ref_line outside function ranges** (20-28% speedup when `ref_line` doesn't match any function)
The speedup is more modest (10-20%) for simple cases where most nodes need visiting anyway, but these cases still benefit from reduced overhead.
Line profiler data confirms the optimization: the original code spent 55.8% of time in `ast.walk()` iteration, while the optimized version eliminates this bottleneck through intelligent pruning. The pruning logic itself adds only 5.8% overhead (lines checking `node_start` and bounds), which is vastly outweighed by the reduction in nodes processed.
Base automatically changed from
refactor/use-function-to-optimize-in-js
to
main
February 3, 2026 01:13
This was referenced Feb 17, 2026
codeflash-ai
Bot
deleted the
codeflash/optimize-pr1256-2026-02-03T00.39.14
branch
February 18, 2026 21:12
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 #1256
If you approve this dependent PR, these changes will be merged into the original PR branch
refactor/use-function-to-optimize-in-js.📄 18% (0.18x) speedup for
_extract_calling_functionincodeflash/code_utils/code_extractor.py⏱️ Runtime :
6.31 milliseconds→5.34 milliseconds(best of224runs)📝 Explanation and details
The optimized code achieves an 18% runtime improvement by replacing the exhaustive AST traversal with a pruned depth-first search (DFS) that skips irrelevant subtrees.
Key optimization: Spatial pruning in AST traversal
The original code uses
ast.walk(tree), which visits every node in the AST regardless of whether it could possibly contain the target function. The optimized version implements a stack-based DFS that prunes entire subtrees when their line ranges don't containref_line:This spatial pruning is highly effective because:
while stackiteration countSecondary optimization: Deferred string splitting
The code also moves
source_code.splitlines()to execute only after finding a matching function, avoiding unnecessary work when no match exists (13 of 50 test cases in the profiler).Performance characteristics based on test results:
The optimization is most effective for:
ref_lineis far outside valid ranges, enabling early pruning)ref_linedoesn't match any function)The speedup is more modest (10-20%) for simple cases where most nodes need visiting anyway, but these cases still benefit from reduced overhead.
Line profiler data confirms the optimization: the original code spent 55.8% of time in
ast.walk()iteration, while the optimized version eliminates this bottleneck through intelligent pruning. The pruning logic itself adds only 5.8% overhead (lines checkingnode_startand bounds), which is vastly outweighed by the reduction in nodes processed.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1256-2026-02-03T00.39.14and push.