Commit 4ab42f6
authored
Optimize AsyncCallInstrumenter.visit_AsyncFunctionDef
The optimized code achieves a **50% speedup** by replacing the expensive `ast.walk()` traversal with a targeted stack-based search in the new `_instrument_statement_fast()` method.
**Key optimizations:**
1. **Custom AST traversal replaces `ast.walk()`**: The original code used `ast.walk(stmt)` which visits *every* node in the AST subtree. The optimized version uses a manual stack-based traversal that only looks for `ast.Await` nodes, significantly reducing the number of nodes examined.
2. **Early termination**: Once an `ast.Await` node matching the target criteria is found, the search immediately breaks and returns, avoiding unnecessary traversal of remaining nodes.
3. **Optimized decorator checking**: The `any()` generator expression is replaced with a simple for-loop that can exit early when a timeout decorator is found, though this provides minimal gains compared to the AST optimization.
**Why this works so well:**
- `ast.walk()` performs a breadth-first traversal of *all* nodes in the AST subtree, which can be hundreds of nodes for complex statements
- The optimized version only examines nodes that could potentially contain `ast.Await` expressions, dramatically reducing the search space
- For large test functions with many statements (as shown in the annotated tests), this optimization scales particularly well - the 500+ await call test cases show **50-53% speedup**
The optimization is most effective for test cases with:
- Large numbers of async function calls (50%+ improvement)
- Complex nested structures with few actual target calls (40%+ improvement)
- Mixed await patterns where only some calls need instrumentation (35%+ improvement)1 parent d8849a5 commit 4ab42f6
1 file changed
+43
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
364 | 372 | | |
365 | 373 | | |
| 374 | + | |
366 | 375 | | |
367 | 376 | | |
368 | 377 | | |
369 | 378 | | |
370 | 379 | | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
371 | 383 | | |
372 | | - | |
| 384 | + | |
373 | 385 | | |
374 | 386 | | |
375 | 387 | | |
| |||
423 | 435 | | |
424 | 436 | | |
425 | 437 | | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
426 | 458 | | |
427 | 459 | | |
428 | 460 | | |
| |||
0 commit comments