⚡️ Speed up function function_kind by 93%
#323
Closed
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.
📄 93% (0.93x) speedup for
function_kindincodeflash/code_utils/static_analysis.py⏱️ Runtime :
1.95 milliseconds→1.01 milliseconds(best of63runs)📝 Explanation and details
Here's an optimized rewrite of your function for both runtime and memory, based on the line profiler output and your code.
Key optimizations:
for _i in range(len(parents) - 1, -1, -1): continue), which does nothing but waste time.parents[0].type in ["FunctionDef", "AsyncFunctionDef"]with a more efficient set membership{...}.parents and parents[0].type == "ClassDef"directly (avoid double-checking parents).forloop with anelseto avoid redundant returns.Here’s the rewritten code.
Explanation of removed code:
for _i in range(len(parents) - 1, -1, -1): continuewas a no-op—removing it increases speed by eliminating unnecessary iterations.in {"FunctionDef", "AsyncFunctionDef"}is O(1) membership instead of O(n) in a list.This preserves all existing comments as per your instruction.
Let me know if you want further alt optimizations or more detail!
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-function_kind-mbtnfbocand push.