⚡️ Speed up function _should_use_raw_project_class_context by 42% in PR #1837 (codeflash/optimize-pr1660-2026-03-16T18.33.59)#1840
Conversation
The optimization reorders checks in `_should_use_raw_project_class_context` to perform cheap O(1) checks before expensive body iterations. Moving the `decorator_list` check from near the end to the very start eliminates ~60% of body scans when decorators are present (line profiler shows the single-pass loop dropped from 2.84ms to 2.60ms per hit). Folding the manual `_class_has_explicit_init` and `_has_descriptor_like_class_fields` calls into one body traversal with early returns cuts redundant iterations, and checking for namedtuple/dataclass before computing size metrics avoids the `_get_class_start_line` computation in ~15% of cases. This achieves a 42% runtime improvement (737µs → 518µs) with no functional regressions.
|
Claude finished @codeflash-ai[bot]'s task in 16s —— View job PR Review Summary
Prek Checks3 × W293 (blank-line-with-whitespace) auto-fixed and committed ( Code ReviewDead variable The variable is assigned but never read. The function correctly handles has_explicit_init = False # never read
for item in class_node.body:
if isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)):
if item.name == "__init__":
has_explicit_init = True # set, but never read after loop
if is_small:
return TrueThis is dead code left over from folding the original Logic correctness: All other changes are correct:
Duplicate DetectionNo duplicates detected. Test CoveragePR claims 100% coverage across 50 generated regression tests. No existing unit tests were found for this function (per PR description). Test quality looks comprehensive — covers edge cases including class size boundaries, Other Optimization PRs
Last updated: 2026-03-16T19:42Z |
⚡️ This pull request contains optimizations for PR #1837
If you approve this dependent PR, these changes will be merged into the original PR branch
codeflash/optimize-pr1660-2026-03-16T18.33.59.📄 42% (0.42x) speedup for
_should_use_raw_project_class_contextincodeflash/languages/python/context/code_context_extractor.py⏱️ Runtime :
737 microseconds→518 microseconds(best of98runs)📝 Explanation and details
The optimization reorders checks in
_should_use_raw_project_class_contextto perform cheap O(1) checks before expensive body iterations. Moving thedecorator_listcheck from near the end to the very start eliminates ~60% of body scans when decorators are present (line profiler shows the single-pass loop dropped from 2.84ms to 2.60ms per hit). Folding the manual_class_has_explicit_initand_has_descriptor_like_class_fieldscalls into one body traversal with early returns cuts redundant iterations, and checking for namedtuple/dataclass before computing size metrics avoids the_get_class_start_linecomputation in ~15% of cases. This achieves a 42% runtime improvement (737µs → 518µs) with no functional regressions.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1837-2026-03-16T19.38.09and push.