Commit c9d4c61
authored
[mypyc] Optimize dunder methods (#17934)
This change gives mypyc the ability to optionally optimize dunder
methods that can guarantee strict adherence to its signature typing. The
optimization allows to bypass vtable for dunder methods in certain cases
that are applicable.
Currently, mypy has adopted the convention of accept dunder methods that
return `NotImplemented` value even when its signature do not reflect
this possibility. With this change and by enabling an special flag,
mypyc will expect strict typing be honored and will unleash more
optimizations like native call without vtable lookup for some cases on
dunder method calls.
For example it could avoid calls to RichCompare Python API making the
code can be fully optimized in by the C compiler when some comparison
with dunders are required.
Example:
```python
@Final
class A:
def __init__(self, x: i32) -> None:
self.x: Final = x
def __lt__(self, other: "A") -> bool:
return self.x < other.x
A(1) < A(2)
```
would produce:
```c
char CPyDef_A_____lt__(PyObject *cpy_r_self, PyObject *cpy_r_other) {
int32_t cpy_r_r0;
int32_t cpy_r_r1;
char cpy_r_r2;
cpy_r_r0 = ((AObject *)cpy_r_self)->_x;
cpy_r_r1 = ((AObject *)cpy_r_other)->_x;
cpy_r_r2 = cpy_r_r0 < cpy_r_r1;
return cpy_r_r2;
}
...
cpy_r_r29 = CPyDef_A_____lt__(cpy_r_r27, cpy_r_r28);
...
```
Instead of:
```c
PyObject *CPyDef_A_____lt__(PyObject *cpy_r_self, PyObject *cpy_r_other) {
int32_t cpy_r_r0;
int32_t cpy_r_r1;
char cpy_r_r2;
PyObject *cpy_r_r3;
cpy_r_r0 = ((AObject *)cpy_r_self)->_x;
cpy_r_r1 = ((AObject *)cpy_r_other)->_x;
cpy_r_r2 = cpy_r_r0 < cpy_r_r1;
cpy_r_r3 = cpy_r_r2 ? Py_True : Py_False;
CPy_INCREF(cpy_r_r3);
return cpy_r_r3;
}
...
cpy_r_r29 = PyObject_RichCompare(cpy_r_r27, cpy_r_r28, 0);
...
```
Default behavior is kept.
Tests run with both of strict typing enabled and disabled.1 parent bd2aafc commit c9d4c61
File tree
11 files changed
+210
-72
lines changed- mypyc
- irbuild
- test-data
- test
11 files changed
+210
-72
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
470 | 470 | | |
471 | 471 | | |
472 | 472 | | |
| 473 | + | |
473 | 474 | | |
474 | 475 | | |
475 | 476 | | |
| |||
509 | 510 | | |
510 | 511 | | |
511 | 512 | | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
512 | 516 | | |
513 | 517 | | |
514 | 518 | | |
| |||
519 | 523 | | |
520 | 524 | | |
521 | 525 | | |
| 526 | + | |
522 | 527 | | |
523 | 528 | | |
524 | 529 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
| |||
801 | 802 | | |
802 | 803 | | |
803 | 804 | | |
804 | | - | |
805 | | - | |
806 | | - | |
807 | | - | |
808 | | - | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
809 | 812 | | |
810 | | - | |
811 | | - | |
812 | | - | |
813 | | - | |
814 | | - | |
815 | | - | |
816 | | - | |
817 | | - | |
818 | | - | |
819 | | - | |
820 | | - | |
821 | 813 | | |
822 | | - | |
823 | | - | |
824 | | - | |
| 814 | + | |
| 815 | + | |
825 | 816 | | |
826 | | - | |
827 | | - | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
828 | 841 | | |
829 | 842 | | |
830 | 843 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
| 100 | + | |
100 | 101 | | |
101 | 102 | | |
102 | 103 | | |
| |||
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
116 | | - | |
117 | | - | |
118 | | - | |
| 117 | + | |
| 118 | + | |
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| |||
416 | 416 | | |
417 | 417 | | |
418 | 418 | | |
419 | | - | |
| 419 | + | |
| 420 | + | |
420 | 421 | | |
421 | 422 | | |
422 | 423 | | |
| |||
481 | 482 | | |
482 | 483 | | |
483 | 484 | | |
484 | | - | |
| 485 | + | |
| 486 | + | |
485 | 487 | | |
486 | 488 | | |
487 | 489 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
| 170 | + | |
170 | 171 | | |
171 | 172 | | |
172 | 173 | | |
| |||
1398 | 1399 | | |
1399 | 1400 | | |
1400 | 1401 | | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
1401 | 1406 | | |
1402 | 1407 | | |
1403 | 1408 | | |
1404 | 1409 | | |
1405 | 1410 | | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
1406 | 1444 | | |
1407 | 1445 | | |
1408 | 1446 | | |
| |||
1558 | 1596 | | |
1559 | 1597 | | |
1560 | 1598 | | |
1561 | | - | |
1562 | | - | |
1563 | | - | |
1564 | | - | |
1565 | | - | |
1566 | | - | |
1567 | | - | |
1568 | | - | |
1569 | | - | |
1570 | | - | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
1571 | 1602 | | |
1572 | 1603 | | |
1573 | 1604 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
207 | 210 | | |
208 | 211 | | |
209 | 212 | | |
| |||
0 commit comments