⚡️ Speed up function function_has_return_statement by 77% in PR #1227 (limit-install-version)
#1235
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 #1227
If you approve this dependent PR, these changes will be merged into the original PR branch
limit-install-version.📄 77% (0.77x) speedup for
function_has_return_statementincodeflash/discovery/functions_to_optimize.py⏱️ Runtime :
1.29 milliseconds→725 microseconds(best of41runs)📝 Explanation and details
The optimization achieves a 77% speedup (from 1.29ms to 725μs) by restructuring the depth-first search to check the most common locations for return statements first, avoiding unnecessary traversal overhead.
Key Optimizations
Fast-path for top-level returns: The optimized version first scans
function_node.bodydirectly before initiating the full DFS. Since most functions with returns have them at the top level, this short-circuits the expensiveast.iter_child_nodes()calls in the majority of cases.Reduced stack initialization overhead: Instead of initializing the stack with
[function_node]and then iterating over its children, the optimized code starts the stack withlist(body), skipping the wrapper function node entirely. This saves one unnecessary iteration.Early empty-body check: By checking
if not bodyupfront, the code avoids creating an empty stack and entering the while loop for functions with no statements.Performance Impact by Test Pattern
The optimization excels when:
The optimization shows minimal or slight regression when:
Line Profiler Evidence
The key improvement is visible in the line profiler:
ast.iter_child_nodes()was called 1,366 times (82.4% of runtime) in the original versus 679 times (73.2% of runtime) in the optimized version - nearly a 50% reduction in expensive child node iterations, achieved by the fast-path detecting returns before the full DFS begins.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1227-2026-02-01T14.30.56and push.