Skip to content

Commit 90b9a1a

Browse files
authored
[mypyc] Use primitives for calls via type aliases (#14784)
Previously type aliases were looked up from the globals dictionary, even if the target was a primitive type such as list. Now type aliases are treated the same as direct references to types in calls, since type aliases are assumed to be immutable. This speeds up the deltablue benchmark by about 4%.
1 parent 43883fa commit 90b9a1a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

mypyc/irbuild/builder.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
Statement,
4242
SymbolNode,
4343
TupleExpr,
44+
TypeAlias,
4445
TypeInfo,
4546
UnaryExpr,
4647
Var,
@@ -1024,7 +1025,8 @@ def call_refexpr_with_args(
10241025

10251026
# Handle data-driven special-cased primitive call ops.
10261027
if callee.fullname and expr.arg_kinds == [ARG_POS] * len(arg_values):
1027-
call_c_ops_candidates = function_ops.get(callee.fullname, [])
1028+
fullname = get_call_target_fullname(callee)
1029+
call_c_ops_candidates = function_ops.get(fullname, [])
10281030
target = self.builder.matching_call_c(
10291031
call_c_ops_candidates, arg_values, expr.line, self.node_type(expr)
10301032
)
@@ -1355,3 +1357,12 @@ def remangle_redefinition_name(name: str) -> str:
13551357
lookups.
13561358
"""
13571359
return name.replace("'", "__redef__")
1360+
1361+
1362+
def get_call_target_fullname(ref: RefExpr) -> str:
1363+
if isinstance(ref.node, TypeAlias):
1364+
# Resolve simple type aliases. In calls they evaluate to the type they point to.
1365+
target = get_proper_type(ref.node.target)
1366+
if isinstance(target, Instance):
1367+
return target.type.fullname
1368+
return ref.fullname

mypyc/test-data/irbuild-lists.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ L0:
8484
x = r0
8585
return 1
8686

87+
[case testNewListEmptyViaAlias]
88+
from typing import List
89+
90+
ListAlias = list
91+
92+
def f() -> None:
93+
x: List[int] = ListAlias()
94+
95+
[out]
96+
def f():
97+
r0, x :: list
98+
L0:
99+
r0 = PyList_New(0)
100+
x = r0
101+
return 1
102+
87103
[case testNewListTwoItems]
88104
from typing import List
89105
def f() -> None:

0 commit comments

Comments
 (0)