⚡️ Speed up function _get_last_two_names by 126% in PR #1861 (codeflash/optimize-pr1860-2026-03-18T08.05.54) - #1862
Conversation
The optimized code returns immediately when two attributes are collected (line 14-15), rather than continuing to traverse the entire attribute chain and performing cleanup checks at the end. This short-circuits deep attribute chains (e.g., `a.b.c.d.e.f.g.h`) after seeing just `g` and `h`, eliminating ~85% of loop iterations in the worst case. Line profiler confirms the while-loop hit count dropped from 1,135 to 172 (85% reduction), directly causing the 126% speedup. The dead code path checking `len(attrs_rev) >= 2` after the loop was also removed since early return makes it unreachable.
|
Claude finished @codeflash-ai[bot]'s task in 16s —— View job PR Review SummaryPrek Checks✅ Code ReviewVerdict: LGTM ✅ The optimization is correct and clean. The change adds an early return in Logic correctness:
The removed dead code path ( The 126% speedup claim is well-supported: the line profiler shows a 85% reduction in while-loop iterations for deep attribute chains (e.g., Duplicate DetectionNo duplicates detected. Test Coverage
Other Optimization PRs Processed
Last updated: 2026-03-18 |
⚡️ This pull request contains optimizations for PR #1861
If you approve this dependent PR, these changes will be merged into the original PR branch
codeflash/optimize-pr1860-2026-03-18T08.05.54.📄 126% (1.26x) speedup for
_get_last_two_namesincodeflash/languages/python/context/code_context_extractor.py⏱️ Runtime :
148 microseconds→65.4 microseconds(best of145runs)📝 Explanation and details
The optimized code returns immediately when two attributes are collected (line 14-15), rather than continuing to traverse the entire attribute chain and performing cleanup checks at the end. This short-circuits deep attribute chains (e.g.,
a.b.c.d.e.f.g.h) after seeing justgandh, eliminating ~85% of loop iterations in the worst case. Line profiler confirms the while-loop hit count dropped from 1,135 to 172 (85% reduction), directly causing the 126% speedup. The dead code path checkinglen(attrs_rev) >= 2after the loop was also removed since early return makes it unreachable.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1861-2026-03-18T08.19.42and push.