⚡️ Speed up method InitDecorator.visit_ClassDef by 15% in PR #1714 (testgen-review)#1771
⚡️ Speed up method InitDecorator.visit_ClassDef by 15% in PR #1714 (testgen-review)#1771codeflash-ai[bot] wants to merge 2 commits into
InitDecorator.visit_ClassDef by 15% in PR #1714 (testgen-review)#1771Conversation
The optimization defers decorator AST construction until it is actually needed by replacing eager allocation with a `None` check. In the original code, `ast.Call(...)` for the decorator was built unconditionally for every target class (245 µs, 12.4% of runtime per profiler line 9), but in ~5% of code paths that decorator is never inserted (e.g. when `__init__` already has the decorator or the class is a dataclass). The optimized version sets `decorator = None` upfront (37 µs) and constructs the decorator inside the `if decorator is None:` branch only when insertion is confirmed, eliminating wasted allocations. Runtime improved 15% overall (36.6 → 31.7 µs) with no correctness regressions across all test cases.
|
Claude finished @codeflash-ai[bot]'s task in 8m 14s —— View job PR Review Summary
Prek Checks
Code ReviewThe optimization is correct. The change defers One minor observation: the
These two paths are structurally separate (loop vs post-loop), so the duplication is an acceptable trade-off for the measured 15% speedup. No bugs found. Duplicate DetectionNo duplicates detected. The decorator construction logic only exists in Test Coverage98% on Missing lines 171–172 ( Optimization PR CheckThis is the only open optimization PR. CI is partially complete — 7 runs still in progress. The one completed failure ( Last updated: 2026-03-04T22:51 UTC |
|
Closing optimization PR: CI is failing (js-esm-async-optimization, js-ts-class-optimization). |
⚡️ This pull request contains optimizations for PR #1714
If you approve this dependent PR, these changes will be merged into the original PR branch
testgen-review.📄 15% (0.15x) speedup for
InitDecorator.visit_ClassDefincodeflash/verification/instrument_codeflash_capture.py⏱️ Runtime :
36.6 microseconds→31.7 microseconds(best of32runs)📝 Explanation and details
The optimization defers decorator AST construction until it is actually needed by replacing eager allocation with a
Nonecheck. In the original code,ast.Call(...)for the decorator was built unconditionally for every target class (245 µs, 12.4% of runtime per profiler line 9), but in ~5% of code paths that decorator is never inserted (e.g. when__init__already has the decorator or the class is a dataclass). The optimized version setsdecorator = Noneupfront (37 µs) and constructs the decorator inside theif decorator is None:branch only when insertion is confirmed, eliminating wasted allocations. Runtime improved 15% overall (36.6 → 31.7 µs) with no correctness regressions across all test cases.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
🔎 Click to see Concolic Coverage Tests
To edit these changes
git checkout codeflash/optimize-pr1714-2026-03-04T22.46.36and push.