Skip to content

Commit

Permalink
[mypyc] Use primitives for calls via type aliases (#14784)
Browse files Browse the repository at this point in the history
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%.
  • Loading branch information
JukkaL authored Mar 2, 2023

Verified

This commit was signed with the committer’s verified signature.
sanjayankur31 Ankur Sinha
1 parent 43883fa commit 90b9a1a
Showing 2 changed files with 28 additions and 1 deletion.
13 changes: 12 additions & 1 deletion mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@
Statement,
SymbolNode,
TupleExpr,
TypeAlias,
TypeInfo,
UnaryExpr,
Var,
@@ -1024,7 +1025,8 @@ def call_refexpr_with_args(

# Handle data-driven special-cased primitive call ops.
if callee.fullname and expr.arg_kinds == [ARG_POS] * len(arg_values):
call_c_ops_candidates = function_ops.get(callee.fullname, [])
fullname = get_call_target_fullname(callee)
call_c_ops_candidates = function_ops.get(fullname, [])
target = self.builder.matching_call_c(
call_c_ops_candidates, arg_values, expr.line, self.node_type(expr)
)
@@ -1355,3 +1357,12 @@ def remangle_redefinition_name(name: str) -> str:
lookups.
"""
return name.replace("'", "__redef__")


def get_call_target_fullname(ref: RefExpr) -> str:
if isinstance(ref.node, TypeAlias):
# Resolve simple type aliases. In calls they evaluate to the type they point to.
target = get_proper_type(ref.node.target)
if isinstance(target, Instance):
return target.type.fullname
return ref.fullname
16 changes: 16 additions & 0 deletions mypyc/test-data/irbuild-lists.test
Original file line number Diff line number Diff line change
@@ -84,6 +84,22 @@ L0:
x = r0
return 1

[case testNewListEmptyViaAlias]
from typing import List

ListAlias = list

def f() -> None:
x: List[int] = ListAlias()

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

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

0 comments on commit 90b9a1a

Please sign in to comment.