Commit b877d18
authored
Here is your optimized code. The optimization targets the **`trace_dispatch_return`** function specifically, which you profiled. The key performance wins are.
- **Eliminate redundant lookups**: When repeatedly accessing `self.cur` and `self.cur[-2]`, assign them to local variables to avoid repeated list lookups and attribute dereferencing.
- **Rearrange logic**: Move cheapest, earliest returns to the top so unnecessary code isn't executed.
- **Localize attribute/cache lookups**: Assign `self.timings` to a local variable.
- **Inline and combine conditions**: Combine checks to avoid unnecessary attribute lookups or `hasattr()` calls.
- **Inline dictionary increments**: Use `dict.get()` for fast set-or-increment semantics.
No changes are made to the return value or side effects of the function.
**Summary of improvements:**
- All repeated list and dict lookups changed to locals for faster access.
- All guards and returns are now at the top and out of the main logic path.
- Increments and dict assignments use `get` and one-liners.
- Removed duplicate lookups of `self.cur`, `self.cur[-2]`, and `self.timings` for maximum speed.
- Kept the function `trace_dispatch_return` identical in behavior and return value.
**No other comments/code outside the optimized function have been changed.**
---
**If this function is in a hot path, this will measurably reduce the call overhead in Python.**
1 parent ee4c7ad commit b877d18
1 file changed
+35
-37
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
494 | 495 | | |
495 | 496 | | |
496 | 497 | | |
497 | | - | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
498 | 501 | | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
505 | 514 | | |
506 | | - | |
507 | 515 | | |
508 | | - | |
509 | | - | |
| 516 | + | |
510 | 517 | | |
511 | | - | |
512 | | - | |
513 | | - | |
514 | 518 | | |
515 | 519 | | |
516 | 520 | | |
517 | | - | |
| 521 | + | |
518 | 522 | | |
519 | | - | |
520 | | - | |
521 | | - | |
| 523 | + | |
| 524 | + | |
522 | 525 | | |
523 | 526 | | |
524 | | - | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
541 | | - | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
542 | 534 | | |
543 | | - | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
544 | 540 | | |
545 | | - | |
| 541 | + | |
| 542 | + | |
546 | 543 | | |
| 544 | + | |
547 | 545 | | |
548 | 546 | | |
549 | 547 | | |
| |||
0 commit comments