Skip to content

Commit

Permalink
Minor proper subtype check optimization
Browse files Browse the repository at this point in the history
Mypyc is bad at compiling nested functions. In one use case
we were spending 7% of the mypy runtime just creating closure
objects for `check_argument`. Here I manually inline the nested
function to avoid this overhead.

Work on #12526.
  • Loading branch information
JukkaL committed Apr 7, 2022
1 parent e7e1db6 commit 6ec79bc
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1365,13 +1365,6 @@ def visit_instance(self, left: Instance) -> bool:
return True

if left.type.has_base(right.type.fullname):
def check_argument(leftarg: Type, rightarg: Type, variance: int) -> bool:
if variance == COVARIANT:
return self._is_proper_subtype(leftarg, rightarg)
elif variance == CONTRAVARIANT:
return self._is_proper_subtype(rightarg, leftarg)
else:
return mypy.sametypes.is_same_type(leftarg, rightarg)
# Map left type to corresponding right instances.
left = map_instance_to_supertype(left, right.type)
if self.erase_instances:
Expand All @@ -1382,7 +1375,13 @@ def check_argument(leftarg: Type, rightarg: Type, variance: int) -> bool:
nominal = True
for ta, ra, tvar in zip(left.args, right.args, right.type.defn.type_vars):
if isinstance(tvar, TypeVarType):
nominal = nominal and check_argument(ta, ra, tvar.variance)
variance = tvar.variant
if variance == COVARIANT:
nominal = nominal and self._is_proper_subtype(ta, ra)
elif variance == CONTRAVARIANT:
nominal = nominal and self._is_proper_subtype(ra, ta)
else:
nominal = nominal and return mypy.sametypes.is_same_type(ta, ra)
else:
nominal = nominal and mypy.sametypes.is_same_type(ta, ra)

Expand Down

0 comments on commit 6ec79bc

Please sign in to comment.