⚡️ Speed up function _class_has_explicit_init by 11% in PR #1839 (codeflash/optimize-pr1838-2026-03-16T19.35.10) - #1841
Conversation
The optimization replaces `isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef))` with direct class identity checks `item.__class__ is ast.FunctionDef or item.__class__ is ast.AsyncFunctionDef`, eliminating tuple allocation and the isinstance method call overhead on every iteration through the class body. Line profiler shows the conditional statement dropped from 460.8 ns/hit to 357.4 ns/hit (22% faster per check), directly driving the 11% overall runtime improvement. The optimization is most effective when scanning classes with many body items that are not init methods, as seen in the 46.7% speedup on the test with 1000 members where init is buried at position 500.
|
Claude finished @codeflash-ai[bot]'s task in 3m 51s —— View job PR Review Summary
Prek ChecksAll prek checks pass (ruff lint + format: ✅). Code Reviewmypy type error — fixed The optimization changed Fix applied: Reverted the line back to Duplicate DetectionNo duplicates detected. This function is unique to Test CoverageThe PR reports 100% coverage from 1063 generated regression tests. No pre-existing unit tests cover this function directly. Other codeflash-ai[bot] PRs
|
|
⚡️ This pull request contains optimizations for PR #1839
If you approve this dependent PR, these changes will be merged into the original PR branch
codeflash/optimize-pr1838-2026-03-16T19.35.10.📄 11% (0.11x) speedup for
_class_has_explicit_initincodeflash/languages/python/context/code_context_extractor.py⏱️ Runtime :
494 microseconds→444 microseconds(best of148runs)📝 Explanation and details
The optimization replaces
isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef))with direct class identity checksitem.__class__ is ast.FunctionDef or item.__class__ is ast.AsyncFunctionDef, eliminating tuple allocation and the isinstance method call overhead on every iteration through the class body. Line profiler shows the conditional statement dropped from 460.8 ns/hit to 357.4 ns/hit (22% faster per check), directly driving the 11% overall runtime improvement. The optimization is most effective when scanning classes with many body items that are not init methods, as seen in the 46.7% speedup on the test with 1000 members where init is buried at position 500.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1839-2026-03-16T19.55.38and push.