⚡️ Speed up function find_target_node by 18% in PR #763 (fix/correctly-find-funtion-node-when-reverting-helpers)
#764
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 #763
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/correctly-find-funtion-node-when-reverting-helpers.📄 18% (0.18x) speedup for
find_target_nodeincodeflash/context/unused_definition_remover.py⏱️ Runtime :
892 microseconds→753 microseconds(best of101runs)📝 Explanation and details
The optimized version eliminates recursive function calls by replacing the recursive
_findhelper with an iterative approach. This provides significant performance benefits:Key Optimizations:
Removed Recursion Overhead: The original code used a recursive helper function
_findthat created new stack frames for each parent traversal. The optimized version uses a simple iterative loop that traverses parents sequentially without function call overhead.Eliminated Function Creation: The original code defined the
_findfunction on every call tofind_target_node. The optimized version removes this repeated function definition entirely.Early Exit with for-else: The optimized code uses Python's
for-elseconstruct to immediately returnNonewhen a parent class isn't found, avoiding unnecessary continued searching.Reduced Attribute Access: By caching
function_to_optimize.function_namein a local variabletarget_nameand reusingbodyvariables, the code reduces repeated attribute lookups.Performance Impact by Test Case:
The optimization is particularly effective for deeply nested class hierarchies where the original recursive approach created multiple stack frames, while the iterative version maintains constant memory usage regardless of nesting depth.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr763-2025-09-25T14.28.58and push.