Skip to content

Commit 989d600

Browse files
authored
Optimize construction via list() & dict() (#10918)
Instead of loading the type and calling it via the vectorcall convention let's use PyList_New / PyDict_New directly to create these empty collections.
1 parent 0bafbfb commit 989d600

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

mypyc/doc/dict_operations.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Construct dict from keys and values:
1313

1414
* ``{key: value, ...}``
1515

16+
Construct empty dict:
17+
18+
* ``{}``
19+
* ``dict()``
20+
1621
Construct dict from another object:
1722

1823
* ``dict(d: dict)``

mypyc/doc/list_operations.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Construct list with specific items:
1313

1414
* ``[item0, ..., itemN]``
1515

16+
Construct empty list:
17+
18+
* ``[]``
19+
* ``list()``
20+
1621
Construct list from iterable:
1722

1823
* ``list(x: Iterable)``

mypyc/primitives/dict_ops.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
type=object_rprimitive,
1818
src='PyDict_Type')
1919

20+
# Construct an empty dictionary via dict().
21+
function_op(
22+
name='builtins.dict',
23+
arg_types=[],
24+
return_type=dict_rprimitive,
25+
c_function_name='PyDict_New',
26+
error_kind=ERR_MAGIC)
27+
2028
# Construct an empty dictionary.
2129
dict_new_op = custom_op(
2230
arg_types=[],

mypyc/primitives/list_ops.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
c_function_name='PySequence_List',
2525
error_kind=ERR_MAGIC)
2626

27+
# Construct an empty list via list().
28+
function_op(
29+
name='builtins.list',
30+
arg_types=[],
31+
return_type=list_rprimitive,
32+
c_function_name='PyList_New',
33+
error_kind=ERR_MAGIC,
34+
extra_int_constants=[(0, int_rprimitive)])
35+
2736
new_list_op = custom_op(
2837
arg_types=[c_pyssize_t_rprimitive],
2938
return_type=list_rprimitive,

mypyc/test-data/irbuild-dict.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ L0:
4242
d = r0
4343
return 1
4444

45+
[case testNewEmptyDictViaFunc]
46+
from typing import Dict
47+
def f() -> None:
48+
d: Dict[bool, int] = dict()
49+
50+
[out]
51+
def f():
52+
r0, d :: dict
53+
L0:
54+
r0 = PyDict_New()
55+
d = r0
56+
return 1
57+
4558
[case testNewDictWithValues]
4659
def f(x: object) -> None:
4760
d = {1: 2, '': x}

mypyc/test-data/irbuild-lists.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ L0:
7070
x = r0
7171
return 1
7272

73+
[case testNewListEmptyViaFunc]
74+
from typing import List
75+
def f() -> None:
76+
x: List[int] = list()
77+
78+
[out]
79+
def f():
80+
r0, x :: list
81+
L0:
82+
r0 = PyList_New(0)
83+
x = r0
84+
return 1
85+
7386
[case testNewListTwoItems]
7487
from typing import List
7588
def f() -> None:

0 commit comments

Comments
 (0)