Skip to content

[mypyc] Optimize construction via list() & dict() #10918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypyc/doc/dict_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Construct dict from keys and values:

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

Construct empty dict:

* ``{}``
* ``dict()``

Construct dict from another object:

* ``dict(d: dict)``
Expand Down
5 changes: 5 additions & 0 deletions mypyc/doc/list_operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Construct list with specific items:

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

Construct empty list:

* ``[]``
* ``list()``

Construct list from iterable:

* ``list(x: Iterable)``
Expand Down
8 changes: 8 additions & 0 deletions mypyc/primitives/dict_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
type=object_rprimitive,
src='PyDict_Type')

# Construct an empty dictionary via dict().
function_op(
name='builtins.dict',
arg_types=[],
return_type=dict_rprimitive,
c_function_name='PyDict_New',
error_kind=ERR_MAGIC)

# Construct an empty dictionary.
dict_new_op = custom_op(
arg_types=[],
Expand Down
9 changes: 9 additions & 0 deletions mypyc/primitives/list_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
c_function_name='PySequence_List',
error_kind=ERR_MAGIC)

# Construct an empty list via list().
function_op(
name='builtins.list',
arg_types=[],
return_type=list_rprimitive,
c_function_name='PyList_New',
error_kind=ERR_MAGIC,
extra_int_constants=[(0, int_rprimitive)])

new_list_op = custom_op(
arg_types=[c_pyssize_t_rprimitive],
return_type=list_rprimitive,
Expand Down
13 changes: 13 additions & 0 deletions mypyc/test-data/irbuild-dict.test
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ L0:
d = r0
return 1

[case testNewEmptyDictViaFunc]
from typing import Dict
def f() -> None:
d: Dict[bool, int] = dict()

[out]
def f():
r0, d :: dict
L0:
r0 = PyDict_New()
d = r0
return 1

[case testNewDictWithValues]
def f(x: object) -> None:
d = {1: 2, '': x}
Expand Down
13 changes: 13 additions & 0 deletions mypyc/test-data/irbuild-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ L0:
x = r0
return 1

[case testNewListEmptyViaFunc]
from typing import List
def f() -> None:
x: List[int] = list()

[out]
def f():
r0, x :: list
L0:
r0 = PyList_New(0)
x = r0
return 1

[case testNewListTwoItems]
from typing import List
def f() -> None:
Expand Down