⚡️ Speed up function _collect_synthetic_constructor_type_names by 33% in PR #1860 (fix/attrs-init-instrumentation)#1865
Conversation
The optimization replaced the expensive set union operator (`|=`) with in-place `set.update()` in two recursive functions, and added an early-exit fast-path to `_is_classvar_annotation` that handles the common case of a simple `ast.Name` annotation without calling the costly `_get_expr_name` helper. Line profiler shows `_is_classvar_annotation` dropped from 6.6 ms to 2.0 ms (70% faster) because 99.6% of calls now hit the fast-path. The `_expr_matches_name` call frequency fell from 1,390 to 74 hits per invocation because ClassVar checks resolve immediately. Additionally, `_expr_matches_name` itself was reordered to test exact equality before constructing the `.suffix` string, saving allocations when matches are found early. Overall runtime improved 32% with no functional changes.
|
Claude finished @codeflash-ai[bot]'s task in 40s —— View job PR Review Summary
Prek Checks✅ Code ReviewThe four changes are all semantically correct micro-optimizations:
No bugs, security issues, or correctness problems found. Duplicate DetectionNo duplicates detected. The three functions modified ( Test Coverage
Optimization PR StatusThis is the only open codeflash-ai optimization PR. CI is still running — eligible to merge once all checks complete. Last updated: 2026-03-18 |
⚡️ This pull request contains optimizations for PR #1860
If you approve this dependent PR, these changes will be merged into the original PR branch
fix/attrs-init-instrumentation.📄 33% (0.33x) speedup for
_collect_synthetic_constructor_type_namesincodeflash/languages/python/context/code_context_extractor.py⏱️ Runtime :
1.61 milliseconds→1.21 milliseconds(best of117runs)📝 Explanation and details
The optimization replaced the expensive set union operator (
|=) with in-placeset.update()in two recursive functions, and added an early-exit fast-path to_is_classvar_annotationthat handles the common case of a simpleast.Nameannotation without calling the costly_get_expr_namehelper. Line profiler shows_is_classvar_annotationdropped from 6.6 ms to 2.0 ms (70% faster) because 99.6% of calls now hit the fast-path. The_expr_matches_namecall frequency fell from 1,390 to 74 hits per invocation because ClassVar checks resolve immediately. Additionally,_expr_matches_nameitself was reordered to test exact equality before constructing the.suffixstring, saving allocations when matches are found early. Overall runtime improved 32% with no functional changes.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1860-2026-03-18T09.30.52and push.