⚡️ Speed up method FunctionCallFinder._get_call_name by 113%
#837
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.
📄 113% (1.13x) speedup for
FunctionCallFinder._get_call_nameincodeflash/code_utils/code_extractor.py⏱️ Runtime :
832 microseconds→391 microseconds(best of40runs)📝 Explanation and details
The optimized version improves performance by replacing the
while isinstance()condition with a tighterwhile Trueloop that explicitly checks types inside the loop body. This eliminates redundantisinstance()calls on each iteration.Key optimizations:
Eliminated redundant type checking: The original code calls
isinstance(current, ast.Attribute)twice per iteration - once in the while condition and again when accessingcurrent.value. The optimized version uses a singleisinstance()check per iteration.More efficient loop structure: Changed from
while isinstance(current, ast.Attribute):towhile True:with explicit type checks and early exits usingcontinueandbreak. This reduces function call overhead on each loop iteration.Direct variable assignment: Uses
val = current.valueonce and reuses it, avoiding repeated property access.Performance impact by test case type:
foo()): ~133% faster due to reduced overhead in the fast pathobj.bar(),pkg.mod.func()): ~114-225% faster, with deeper chains seeing more benefitThe optimization is particularly effective for attribute chain resolution, which is common in method calls and module imports - the primary use case for this AST analysis code.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-FunctionCallFinder._get_call_name-mgzrorsjand push.