Skip to content

Commit 71942c0

Browse files
authored
[mypyc] Use non-tagged integer for generator label (#19218)
Also consider it as always defined to generate simpler code. This appears to speed up a simple benchmark by 3%, but it could be noise. This reduces the volume of generated code -- the line count of a small compiled program with a few async functions was reduced by 5%.
1 parent dd1f2a3 commit 71942c0

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

mypyc/irbuild/builder.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,12 +1300,19 @@ def node_type(self, node: Expression) -> RType:
13001300
return self.type_to_rtype(mypy_type)
13011301

13021302
def add_var_to_env_class(
1303-
self, var: SymbolNode, rtype: RType, base: FuncInfo | ImplicitClass, reassign: bool = False
1303+
self,
1304+
var: SymbolNode,
1305+
rtype: RType,
1306+
base: FuncInfo | ImplicitClass,
1307+
reassign: bool = False,
1308+
always_defined: bool = False,
13041309
) -> AssignmentTarget:
13051310
# First, define the variable name as an attribute of the environment class, and then
13061311
# construct a target for that attribute.
13071312
name = remangle_redefinition_name(var.name)
13081313
self.fn_info.env_class.attributes[name] = rtype
1314+
if always_defined:
1315+
self.fn_info.env_class.attrs_with_defaults.add(name)
13091316
attr_target = AssignmentTargetAttr(base.curr_env_reg, name)
13101317

13111318
if reassign:

mypyc/irbuild/generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
Unreachable,
3333
Value,
3434
)
35-
from mypyc.ir.rtypes import RInstance, int_rprimitive, object_rprimitive
35+
from mypyc.ir.rtypes import RInstance, int32_rprimitive, object_rprimitive
3636
from mypyc.irbuild.builder import IRBuilder, calculate_arg_defaults, gen_arg_defaults
3737
from mypyc.irbuild.context import FuncInfo, GeneratorClass
3838
from mypyc.irbuild.env_class import (
@@ -415,7 +415,7 @@ def setup_env_for_generator_class(builder: IRBuilder) -> None:
415415
# the '__next__' function of the generator is called, and add it
416416
# as an attribute to the environment class.
417417
cls.next_label_target = builder.add_var_to_env_class(
418-
Var(NEXT_LABEL_ATTR_NAME), int_rprimitive, cls, reassign=False
418+
Var(NEXT_LABEL_ATTR_NAME), int32_rprimitive, cls, reassign=False, always_defined=True
419419
)
420420

421421
# Add arguments from the original generator function to the

0 commit comments

Comments
 (0)