⚡️ Speed up function _expr_matches_name by 26% in PR #1660 (unstructured-inference)#1850
Conversation
The optimization replaced recursive calls in `_get_expr_name` with an iterative loop that walks attribute chains once, collecting parts into a list and reversing them only at the end, eliminating function-call overhead that dominated 46% of original runtime (line profiler shows recursive calls at 1154 ns/hit vs. the new loop iterations at ~300 ns/hit). Additionally, `_expr_matches_name` now precomputes `"." + suffix` once instead of building it twice per invocation via f-strings, cutting redundant string allocations. The net 26% runtime improvement comes primarily from avoiding Python's recursion stack and reducing temporary object creation in the hot path, with all tests passing and only minor per-test slowdowns (typically 10–25%) offset by dramatic wins on deep attribute chains (up to 393% faster for 100-level nesting).
|
Claude finished @codeflash-ai[bot]'s task in 25s —— View job PR Review Summary
Prek Checks✅ ✅ Stale ThreadsNo unresolved Claude review threads found. Code ReviewThe optimization converts
Minor observations (no action required):
Overall: The optimization is correct and well-tested (231 generated regression tests + 1 replay test, 100% coverage). The 26% speedup claim on deep attribute chains is plausible given Python's function call overhead. Duplicate DetectionNo duplicates detected. Test CoveragePer the PR body: 100% coverage reported by CodeFlash's correctness verification. No existing unit tests for these private helpers, but 231 generated regression tests exercise all code paths. Optimization PRsFound 2 open codeflash-ai[bot] PRs:
|
⚡️ This pull request contains optimizations for PR #1660
If you approve this dependent PR, these changes will be merged into the original PR branch
unstructured-inference.📄 26% (0.26x) speedup for
_expr_matches_nameincodeflash/languages/python/context/code_context_extractor.py⏱️ Runtime :
419 microseconds→333 microseconds(best of5runs)⚡️ This change will improve the performance of the following benchmarks:
{benchmark_info_improved}
🔻 This change will degrade the performance of the following benchmarks:
📝 Explanation and details
The optimization replaced recursive calls in
_get_expr_namewith an iterative loop that walks attribute chains once, collecting parts into a list and reversing them only at the end, eliminating function-call overhead that dominated 46% of original runtime (line profiler shows recursive calls at 1154 ns/hit vs. the new loop iterations at ~300 ns/hit). Additionally,_expr_matches_namenow precomputes"." + suffixonce instead of building it twice per invocation via f-strings, cutting redundant string allocations. The net 26% runtime improvement comes primarily from avoiding Python's recursion stack and reducing temporary object creation in the hot path, with all tests passing and only minor per-test slowdowns (typically 10–25%) offset by dramatic wins on deep attribute chains (up to 393% faster for 100-level nesting).✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
⏪ Click to see Replay Tests
benchmarks/codeflash_replay_tests_260k0cbn/test_tests_benchmarks_test_benchmark_code_extract_code_context__replay_test_0.py::test_codeflash_languages_python_context_code_context_extractor__expr_matches_name_test_benchmark_extractTo edit these changes
git checkout codeflash/optimize-pr1660-2026-03-17T03.31.35and push.