Commit 58de753
authored
[mypyc] Reduce impact of immortality on reference counting performance (#18459)
Fixes mypyc/mypyc#1044.
The addition of object immortality in Python 3.12 (PEP 683) introduced
an extra immortality check to incref and decref operations. Objects with
a specific reference count are treated as immortal, and their reference
counts are never updated.
It turns out that this slowed down the performance of certain workloads
a lot (up to 70% increase in runtime, compared to 3.11). This PR reduces
the impact of immortality via a few optimizations:
1. Assume instances of native classes and list objects are not immortal
(skip immortality checks).
2. Skip incref of certain objects in some contexts when we know that
they are immortal (e.g. avoid incref of `None`).
The second change should be clear. We generally depend on CPython
implementation details to improve performance, and this seems safe to do
here as well.
The first change could turn immortal objects into non-immortal ones. For
native classes this is a decision we can arguably make -- native classes
don't properly support immortality, and they can't be shared between
subinterpreters. As discussed in PEP 683, skipping immortality checks
here is acceptable even in cases where somebody tries to make a native
instance immortal, but this could have some performance or memory use
impact. The performance gains make this a good tradeoff.
Since lists are mutable, they can't be safely shared between
subinterpreters, so again not dealing with immortality is acceptable. It
could reduce performance in some use cases by deimmortalizing lists, but
this potential impact seems marginal compared to faster incref and
decref operations on lists, which are some of the more common objects in
Python programs.
This speeds up self check by about 1.5% on Python 3.13. This speeds up
the richards benchmark by 30-35% (!) on 3.13, and also some other
benchmarks see smaller improvements.1 parent 43ea203 commit 58de753
File tree
7 files changed
+241
-18
lines changed- mypyc
- codegen
- ir
- lib-rt
- test
7 files changed
+241
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
511 | 512 | | |
512 | 513 | | |
513 | 514 | | |
514 | | - | |
515 | | - | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
516 | 520 | | |
517 | 521 | | |
518 | 522 | | |
| |||
540 | 544 | | |
541 | 545 | | |
542 | 546 | | |
543 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
544 | 551 | | |
545 | 552 | | |
546 | 553 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| 80 | + | |
79 | 81 | | |
80 | 82 | | |
81 | 83 | | |
| 84 | + | |
82 | 85 | | |
83 | 86 | | |
84 | 87 | | |
| |||
578 | 581 | | |
579 | 582 | | |
580 | 583 | | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
581 | 599 | | |
582 | 600 | | |
583 | 601 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
85 | 91 | | |
86 | 92 | | |
87 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
| |||
151 | 156 | | |
152 | 157 | | |
153 | 158 | | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
154 | 163 | | |
155 | 164 | | |
156 | 165 | | |
| |||
193 | 202 | | |
194 | 203 | | |
195 | 204 | | |
| 205 | + | |
196 | 206 | | |
197 | 207 | | |
198 | 208 | | |
| |||
204 | 214 | | |
205 | 215 | | |
206 | 216 | | |
| 217 | + | |
207 | 218 | | |
208 | 219 | | |
209 | 220 | | |
| |||
230 | 241 | | |
231 | 242 | | |
232 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
233 | 248 | | |
234 | 249 | | |
235 | 250 | | |
| |||
433 | 448 | | |
434 | 449 | | |
435 | 450 | | |
436 | | - | |
437 | | - | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
438 | 457 | | |
439 | 458 | | |
440 | 459 | | |
| |||
642 | 661 | | |
643 | 662 | | |
644 | 663 | | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
645 | 668 | | |
646 | 669 | | |
647 | 670 | | |
| |||
763 | 786 | | |
764 | 787 | | |
765 | 788 | | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
766 | 793 | | |
767 | 794 | | |
768 | 795 | | |
| |||
823 | 850 | | |
824 | 851 | | |
825 | 852 | | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
826 | 857 | | |
827 | 858 | | |
828 | 859 | | |
| |||
883 | 914 | | |
884 | 915 | | |
885 | 916 | | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
886 | 921 | | |
887 | 922 | | |
888 | 923 | | |
| |||
953 | 988 | | |
954 | 989 | | |
955 | 990 | | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
956 | 995 | | |
957 | 996 | | |
958 | 997 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
34 | 63 | | |
35 | 64 | | |
36 | 65 | | |
| |||
0 commit comments