Commit e766d0a
authored
Optimize normalize_code
The optimization replaces the `remove_docstrings_from_ast` function with `fast_remove_docstrings_from_ast` that uses a more efficient traversal strategy.
**Key optimizations:**
1. **Eliminates `ast.walk()` overhead**: The original code uses `ast.walk()` which visits every single node in the AST tree (21,611 hits in profiler). The optimized version uses a custom stack-based traversal that only visits nodes that can actually contain docstrings.
2. **Targeted traversal**: Instead of examining all AST nodes, the optimized version only traverses `FunctionDef`, `AsyncFunctionDef`, `ClassDef`, and `Module` nodes - the only node types that can contain docstrings in their `body[0]` position.
3. **Reduced function call overhead**: The stack-based approach eliminates the overhead of `ast.walk()`'s generator-based iteration, reducing the number of Python function calls from 21,611 to just the nodes that matter.
**Performance impact**: The docstring removal step drops from 131.4ms (25.5% of total time) to just 3.07ms (0.8% of total time) - a **97.7% reduction** in that specific operation.
**Test case effectiveness**: The optimization shows consistent 10-25% speedups across all test cases, with the largest gains (23-24%) appearing in tests with many variables or docstrings (`test_large_many_variables_*`, `test_large_docstring_removal_scaling`). Even simple cases benefit from the reduced AST traversal overhead.
The optimization is particularly effective for code with deep nesting or many function/class definitions, as it avoids visiting irrelevant leaf nodes like literals, operators, and expressions that cannot contain docstrings.1 parent 47f4d76 commit e766d0a
1 file changed
+26
-2
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 | | |
| |||
172 | 173 | | |
173 | 174 | | |
174 | 175 | | |
175 | | - | |
| 176 | + | |
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
| |||
233 | 234 | | |
234 | 235 | | |
235 | 236 | | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
0 commit comments