Commit 0f51ff7
authored
Optimize InitDecorator.visit_ClassDef
The optimized code achieves a **65% speedup** through strategic precomputation of AST nodes that are repeatedly created during class processing.
**Key optimizations:**
1. **Precomputed AST components in `__init__`**: Instead of reconstructing identical AST nodes (like `ast.Name`, `ast.arg`, `ast.Constant`) on every `visit_ClassDef` call, the optimized version creates them once during initialization and reuses them. This eliminates the expensive AST node construction overhead seen in the profiler - lines creating decorator keywords and super() call components dropped from ~2ms total to ~0.6ms.
2. **Optimized decorator presence check**: Replaced the `any()` generator expression with a `for/else` loop that stops immediately when finding an existing `codeflash_capture` decorator. This avoids generator allocation overhead and short-circuits the search earlier.
3. **Reduced per-class AST construction**: The decorator is now built once per class using precomputed components, rather than reconstructing all keywords and function references from scratch each time.
**Performance impact by test type:**
- **Basic cases** (single class with simple `__init__`): ~140-220% faster, benefiting from reduced AST node construction
- **Edge cases** (classes needing synthetic `__init__`): ~100-150% faster, particularly benefiting from prebuilt super() call components
- **Large scale** (many methods/classes): ~17-40% faster, where the constant-time optimizations compound across many iterations
The optimization is most effective for workloads processing many classes, as the upfront precomputation cost is amortized across multiple `visit_ClassDef` calls, directly addressing the bottleneck of repetitive AST node creation identified in the profiler.1 parent 824d8f6 commit 0f51ff7
1 file changed
+37
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
95 | 116 | | |
96 | 117 | | |
97 | 118 | | |
| |||
114 | 135 | | |
115 | 136 | | |
116 | 137 | | |
117 | | - | |
118 | 138 | | |
119 | | - | |
120 | | - | |
| 139 | + | |
121 | 140 | | |
122 | | - | |
| 141 | + | |
123 | 142 | | |
124 | 143 | | |
125 | 144 | | |
126 | | - | |
127 | | - | |
128 | | - | |
| 145 | + | |
129 | 146 | | |
130 | 147 | | |
131 | 148 | | |
| 149 | + | |
132 | 150 | | |
133 | 151 | | |
134 | 152 | | |
| |||
139 | 157 | | |
140 | 158 | | |
141 | 159 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
147 | 166 | | |
148 | 167 | | |
149 | 168 | | |
150 | 169 | | |
151 | | - | |
| 170 | + | |
152 | 171 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
| 172 | + | |
162 | 173 | | |
163 | | - | |
| 174 | + | |
164 | 175 | | |
165 | 176 | | |
166 | | - | |
167 | | - | |
| 177 | + | |
| 178 | + | |
168 | 179 | | |
169 | 180 | | |
170 | | - | |
| 181 | + | |
171 | 182 | | |
172 | 183 | | |
173 | 184 | | |
| |||
0 commit comments