Skip to content

Commit a1782de

Browse files
committed
Revert optional type special casing
I'm not convinced that it's any better. Getting the address of None requires a memory read, so we'd have an extra memory read in the slow path.
1 parent fdc6e7b commit a1782de

File tree

2 files changed

+2
-41
lines changed

2 files changed

+2
-41
lines changed

mypyc/codegen/emit.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -514,18 +514,7 @@ def emit_inc_ref(self, dest: str, rtype: RType, *, rare: bool = False) -> None:
514514
elif not rtype.is_unboxed:
515515
# Always inline, since this is a simple but very hot op
516516
if rtype.may_be_immortal or not HAVE_IMMORTAL:
517-
if (
518-
HAVE_IMMORTAL
519-
and (opt := optional_value_type(rtype))
520-
and not opt.may_be_immortal
521-
):
522-
# Optimized incref of optional type avoids reading reference count
523-
# if value is None (None is immortal)
524-
self.emit_line(f"if ({dest} != Py_None) {{")
525-
self.emit_line(f"CPy_INCREF_NO_IMM({dest});")
526-
self.emit_line("}")
527-
else:
528-
self.emit_line("CPy_INCREF(%s);" % dest)
517+
self.emit_line("CPy_INCREF(%s);" % dest)
529518
else:
530519
self.emit_line("CPy_INCREF_NO_IMM(%s);" % dest)
531520
# Otherwise assume it's an unboxed, pointerless value and do nothing.
@@ -556,18 +545,7 @@ def emit_dec_ref(
556545
else:
557546
# Inlined
558547
if rtype.may_be_immortal or not HAVE_IMMORTAL:
559-
if (
560-
HAVE_IMMORTAL
561-
and (opt := optional_value_type(rtype))
562-
and not opt.may_be_immortal
563-
):
564-
# Optimized decref of optional type avoids reading reference count
565-
# if value is None (None is immortal)
566-
self.emit_line(f"if ({dest} != Py_None) {{")
567-
self.emit_line(f"CPy_{x}DECREF_NO_IMM({dest});")
568-
self.emit_line("}")
569-
else:
570-
self.emit_line(f"CPy_{x}DECREF({dest});")
548+
self.emit_line(f"CPy_{x}DECREF({dest});")
571549
else:
572550
self.emit_line(f"CPy_{x}DECREF_NO_IMM({dest});")
573551
# Otherwise assume it's an unboxed, pointerless value and do nothing.

mypyc/test/test_emit.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
RTuple,
1212
RUnion,
1313
bool_rprimitive,
14-
dict_rprimitive,
1514
int_rprimitive,
1615
list_rprimitive,
1716
none_rprimitive,
@@ -117,14 +116,6 @@ def test_emit_inc_ref_instance(self) -> None:
117116
def test_emit_inc_ref_optional(self) -> None:
118117
optional = RUnion([self.instance_a, none_rprimitive])
119118
self.emitter.emit_inc_ref("o", optional)
120-
if HAVE_IMMORTAL:
121-
self.assert_output("if (o != Py_None) {\n CPy_INCREF_NO_IMM(o);\n}\n")
122-
else:
123-
self.assert_output("CPy_INCREF(o);\n")
124-
125-
def test_emit_inc_ref_optional_2(self) -> None:
126-
optional = RUnion([dict_rprimitive, none_rprimitive])
127-
self.emitter.emit_inc_ref("o", optional)
128119
self.assert_output("CPy_INCREF(o);\n")
129120

130121
def test_emit_dec_ref_object(self) -> None:
@@ -172,14 +163,6 @@ def test_emit_dec_ref_instance(self) -> None:
172163
def test_emit_dec_ref_optional(self) -> None:
173164
optional = RUnion([self.instance_a, none_rprimitive])
174165
self.emitter.emit_dec_ref("o", optional)
175-
if HAVE_IMMORTAL:
176-
self.assert_output("if (o != Py_None) {\n CPy_DECREF_NO_IMM(o);\n}\n")
177-
else:
178-
self.assert_output("CPy_DECREF(o);\n")
179-
180-
def test_emit_dec_ref_optional_2(self) -> None:
181-
optional = RUnion([dict_rprimitive, none_rprimitive])
182-
self.emitter.emit_dec_ref("o", optional)
183166
self.assert_output("CPy_DECREF(o);\n")
184167

185168
def assert_output(self, expected: str) -> None:

0 commit comments

Comments
 (0)