Commit 7a2265f
authored
Optimize AsyncCallInstrumenter._process_test_function
The optimization achieves a **12% speedup** through several targeted improvements in the `_process_test_function` and `_instrument_statement` methods:
**Key Optimizations:**
1. **Variable hoisting and local references**: The optimized code extracts frequently accessed instance variables (`self.async_call_counter`, `node.name`) into local variables at the beginning of `_process_test_function`. It also creates local references to methods (`self._instrument_statement`, `new_body.append`) to avoid repeated attribute lookups during the main loop.
2. **Improved timeout decorator check**: Instead of using `any()` with a generator expression, the optimization uses an explicit loop with early termination when a timeout decorator is found. This avoids creating unnecessary generator objects and allows for faster short-circuiting.
3. **Optimized AST traversal**: The most significant improvement is replacing `ast.walk()` with a manual stack-based traversal using `ast.iter_child_nodes()` in `_instrument_statement`. This eliminates the overhead of `ast.walk()`'s recursive generator and provides better control over the traversal process.
4. **Simplified counter management**: The optimization tracks the call index locally during processing and only updates the instance variable once at the end, reducing dictionary access overhead.
**Performance Impact by Test Case:**
- **Small functions**: 61-130% faster for basic test cases with minimal statements
- **Empty/simple functions**: 71-119% faster due to reduced overhead in the main processing loop
- **Large-scale functions**: 11.5% faster for functions with 500+ await statements, where the AST traversal optimization becomes most beneficial
The optimizations are particularly effective for functions with many statements where the improved AST traversal and reduced attribute lookups compound to significant savings.1 parent 40c4108 commit 7a2265f
1 file changed
+49
-28
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 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
364 | 376 | | |
365 | 377 | | |
366 | | - | |
367 | | - | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
368 | 381 | | |
369 | 382 | | |
370 | 383 | | |
371 | | - | |
372 | | - | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
373 | 387 | | |
| 388 | + | |
| 389 | + | |
374 | 390 | | |
375 | | - | |
376 | | - | |
| 391 | + | |
| 392 | + | |
377 | 393 | | |
378 | 394 | | |
379 | 395 | | |
| |||
388 | 404 | | |
389 | 405 | | |
390 | 406 | | |
391 | | - | |
| 407 | + | |
392 | 408 | | |
393 | 409 | | |
394 | | - | |
| 410 | + | |
395 | 411 | | |
| 412 | + | |
396 | 413 | | |
397 | 414 | | |
398 | 415 | | |
399 | 416 | | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
410 | 431 | | |
411 | 432 | | |
412 | 433 | | |
| |||
0 commit comments