Commit 7abd108
authored
Optimize get_code_fingerprint
The optimization achieves a **20% speedup** by **inlining the docstring removal logic** and replacing the expensive `ast.walk()` traversal with a more efficient iterative approach.
**Key changes:**
- **Eliminated function call overhead**: Removed the separate `remove_docstrings_from_ast()` function call, saving function invocation costs
- **Optimized AST traversal**: Replaced `ast.walk()` with an explicit stack-based traversal that only visits nodes that can contain docstrings (`FunctionDef`, `AsyncFunctionDef`, `ClassDef`, `Module`)
- **Reduced allocations**: The iterative approach creates fewer temporary objects compared to `ast.walk()`'s recursive generator pattern
**Performance impact by test case type:**
- **Large-scale tests** (500+ variables/functions) see the biggest gains: **19-25% faster** - these benefit most from the reduced traversal overhead
- **Basic tests** with simple functions: **7-12% faster** - modest but consistent improvement
- **Edge cases** (empty code, syntax errors): **6-9% faster** or minimal impact
The optimization specifically targets the docstring removal step, which previously consumed **24.4%** of total runtime in the line profiler. By making the traversal more targeted and eliminating unnecessary node visits, the optimized version reduces this bottleneck while preserving identical functionality and output.1 parent 47f4d76 commit 7abd108
1 file changed
+32
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
151 | 151 | | |
152 | 152 | | |
153 | 153 | | |
154 | | - | |
| 154 | + | |
| 155 | + | |
155 | 156 | | |
156 | 157 | | |
157 | 158 | | |
| |||
167 | 168 | | |
168 | 169 | | |
169 | 170 | | |
170 | | - | |
171 | 171 | | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
174 | 174 | | |
175 | | - | |
176 | | - | |
177 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
178 | 201 | | |
179 | 202 | | |
180 | 203 | | |
181 | | - | |
| 204 | + | |
| 205 | + | |
182 | 206 | | |
183 | 207 | | |
184 | | - | |
| 208 | + | |
185 | 209 | | |
186 | 210 | | |
187 | 211 | | |
| |||
0 commit comments