⚡️ Speed up method FunctionCallFinder._extract_source_code by 51% in PR #824 (opt-impact-aseem-improvement)
#828
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 #824
If you approve this dependent PR, these changes will be merged into the original PR branch
opt-impact-aseem-improvement.📄 51% (0.51x) speedup for
FunctionCallFinder._extract_source_codeincodeflash/code_utils/code_extractor.py⏱️ Runtime :
1.12 milliseconds→742 microseconds(best of59runs)📝 Explanation and details
The optimized version achieves a 50% speedup through several key micro-optimizations targeting the most expensive operations:
Key Optimizations:
Reduced
line.strip()calls: The original code calledline.strip()twice per line (once for empty line check, once for dedenting logic). The optimization cachesstripped = line.strip()in the min_indent loop, eliminating redundant string operations.Replaced
min()with conditional check: Changedmin_indent = min(min_indent, indent)toif indent < min_indent: min_indent = indent, avoiding function call overhead in the tight loop.Eliminated branch logic in dedenting: The original code had separate loops for class methods vs top-level functions. The optimization unifies this by precomputing
dedent_amountonce, then using a single conditional to choose between dedenting or direct copying.Method reference caching:
append = result_lines.appendavoids repeated attribute lookups in the result building loop.Optimized slicing logic: Removed the
len(line) > dedent_amountcheck since Python slicing past string end safely returns empty string.Performance Impact by Test Case:
The optimizations primarily target the string processing bottlenecks (line stripping, indentation calculation, result building) which dominate runtime for functions with many lines, making this especially effective for large code extraction scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr824-2025-10-17T00.51.58and push.