⚡️ Speed up function find_functions_with_return_statement by 26% in PR #1460 (call-graphee)#1534
Merged
KRRT7 merged 1 commit intocall-grapheefrom Feb 18, 2026
Conversation
The optimized code achieves a **26% runtime improvement** by making the AST traversal in `function_has_return_statement` more targeted and efficient.
**Key Optimization:**
The critical change is in how `function_has_return_statement` traverses the AST when searching for `Return` nodes:
**Original approach:**
```python
stack.extend(ast.iter_child_nodes(node))
```
This visits *all* child nodes including expressions, names, constants, and other non-statement nodes.
**Optimized approach:**
```python
for child in ast.iter_child_nodes(node):
if isinstance(child, ast.stmt):
stack.append(child)
```
This only pushes statement nodes onto the stack, since `Return` is a statement type (`ast.stmt`).
**Why This Is Faster:**
1. **Reduced Node Traversal**: In typical Python functions, there are many more expression nodes (variable references, literals, operators, etc.) than statement nodes. For example, a simple `return x + y` has 1 Return statement but multiple Name and BinOp expression nodes underneath. The optimization skips all the expression-level nodes.
2. **Lower Python Overhead**: Fewer nodes in the stack means fewer loop iterations, fewer `isinstance` checks on non-Return nodes, and less list manipulation overhead.
3. **Preserved Correctness**: Since `Return` nodes are always statements in Python's AST (they inherit from `ast.stmt`), filtering to only statement nodes cannot miss any Return nodes.
**Performance Impact by Test Case:**
The optimization shows particularly strong gains for:
- **Functions without returns** (up to 91% faster): Early termination without traversing deep expression trees
- **Large codebases** (34-41% faster on tests with 1000+ functions): The cumulative effect across many function bodies
- **Functions with complex expressions but no returns** (82% faster): Avoiding expensive traversal of unused expression subtrees
- **Generator functions without explicit returns** (64% faster): Skipping yield expression internals
The optimization maintains correctness across all test cases including nested classes, async functions, properties, and various control structures, while delivering consistent runtime improvements.
2 tasks
Contributor
PR Review SummaryPrek Checks✅ All checks pass — Mypy✅ No new mypy errors introduced by this PR. All mypy errors in the output are pre-existing in files pulled in transitively (e.g., Code ReviewNo critical bugs, security vulnerabilities, or breaking API changes found. This PR introduces a Jedi-based
Test Coverage
Notes:
Last updated: 2026-02-18T22:50Z |
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 #1460
If you approve this dependent PR, these changes will be merged into the original PR branch
call-graphee.📄 26% (0.26x) speedup for
find_functions_with_return_statementincodeflash/discovery/functions_to_optimize.py⏱️ Runtime :
12.0 milliseconds→9.48 milliseconds(best of46runs)📝 Explanation and details
The optimized code achieves a 26% runtime improvement by making the AST traversal in
function_has_return_statementmore targeted and efficient.Key Optimization:
The critical change is in how
function_has_return_statementtraverses the AST when searching forReturnnodes:Original approach:
This visits all child nodes including expressions, names, constants, and other non-statement nodes.
Optimized approach:
This only pushes statement nodes onto the stack, since
Returnis a statement type (ast.stmt).Why This Is Faster:
Reduced Node Traversal: In typical Python functions, there are many more expression nodes (variable references, literals, operators, etc.) than statement nodes. For example, a simple
return x + yhas 1 Return statement but multiple Name and BinOp expression nodes underneath. The optimization skips all the expression-level nodes.Lower Python Overhead: Fewer nodes in the stack means fewer loop iterations, fewer
isinstancechecks on non-Return nodes, and less list manipulation overhead.Preserved Correctness: Since
Returnnodes are always statements in Python's AST (they inherit fromast.stmt), filtering to only statement nodes cannot miss any Return nodes.Performance Impact by Test Case:
The optimization shows particularly strong gains for:
The optimization maintains correctness across all test cases including nested classes, async functions, properties, and various control structures, while delivering consistent runtime improvements.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1460-2026-02-18T22.22.36and push.