⚡️ Speed up method InitDecorator.visit_ClassDef by 10% in PR #1860 (fix/attrs-init-instrumentation)#1867
Conversation
The optimization eliminates redundant iterations through `node.body` by adding a `break` statement immediately after finding and decorating the `__init__` method (when `has_init=True`). The profiler shows the outer body loop dropped from 392 hits to 376 hits (~4% fewer), and the inner decorator-list loop dropped from 18 hits to 18 hits but now exits cleanly via `break` instead of continuing to scan remaining body items. Additionally, the `if not has_init:` branch now consolidates dataclass/attrs/NamedTuple checks in a single decorator loop instead of three separate passes, reducing `_expr_name` calls from 471 total hits to 263 (~44% fewer) and cutting that function's time from 391 µs to 218 µs. Runtime improved from 405 µs to 367 µs (10% faster) with no correctness regressions across all test cases.
|
Claude finished @codeflash-ai[bot]'s task in 45s —— View job PR Review SummaryTodo
Prek Checks✅ Code ReviewThe optimization is sound. Two changes are made: 1. for item in node.body:
if isinstance(item, ast.FunctionDef) and item.name == "__init__" ...:
...
break # ← newCorrect. Python classes can only have one valid 2. Consolidated decorator loop (dataclass + attrs combined) Original had two separate passes over Minor issue — duplicate stale comment at line 222: # Create super().__init__(*args, **kwargs) call (use prebuilt AST fragments) ← misplaced
# Skip NamedTuples ...
for base in node.bases:
...
# Create super().__init__(*args, **kwargs) call (use prebuilt AST fragments) ← correct location
super_call = self._super_call_exprThe first copy of this comment (line 222) is a leftover from the refactor — it appears before the NamedTuple check but has nothing to do with it. The second copy (line 230) is at the correct location. This is cosmetic only and doesn't affect correctness. Fix this → Ordering change: NamedTuple base-class check is now performed after the decorator loop rather than between the two former decorator loops. Semantically equivalent for all realistic inputs. No bugs or correctness regressions found. Duplicate DetectionNo duplicates detected. Test Coverage
Optimization PRs
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.📄 10% (0.10x) speedup for
InitDecorator.visit_ClassDefincodeflash/languages/python/instrument_codeflash_capture.py⏱️ Runtime :
405 microseconds→367 microseconds(best of250runs)📝 Explanation and details
The optimization eliminates redundant iterations through
node.bodyby adding abreakstatement immediately after finding and decorating the__init__method (whenhas_init=True). The profiler shows the outer body loop dropped from 392 hits to 376 hits (~4% fewer), and the inner decorator-list loop dropped from 18 hits to 18 hits but now exits cleanly viabreakinstead of continuing to scan remaining body items. Additionally, theif not has_init:branch now consolidates dataclass/attrs/NamedTuple checks in a single decorator loop instead of three separate passes, reducing_expr_namecalls from 471 total hits to 263 (~44% fewer) and cutting that function's time from 391 µs to 218 µs. Runtime improved from 405 µs to 367 µs (10% faster) with no correctness regressions across all test cases.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1860-2026-03-18T10.39.52and push.