Skip to content

Commit cb44cf0

Browse files
committed
Eliminate redundant refcounting from _BINARY_OP_SUBSCR_TUPLE_INT
1 parent c4ab024 commit cb44cf0

File tree

9 files changed

+65
-34
lines changed

9 files changed

+65
-34
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_ids.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_capi/test_opt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,25 @@ def testfunc(n):
30983098
self.assertNotIn("_POP_TOP_INT", uops)
30993099
self.assertIn("_POP_TOP_NOP", uops)
31003100

3101+
def test_binary_subscr_tuple_int(self):
3102+
def testfunc(n):
3103+
t = (1,)
3104+
x = 0
3105+
for _ in range(n):
3106+
y = t[0]
3107+
x += y
3108+
return x
3109+
3110+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
3111+
self.assertEqual(res, TIER2_THRESHOLD)
3112+
self.assertIsNotNone(ex)
3113+
uops = get_opnames(ex)
3114+
3115+
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
3116+
self.assertNotIn("_POP_TOP", uops)
3117+
self.assertNotIn("_POP_TOP_INT", uops)
3118+
self.assertIn("_POP_TOP_NOP", uops)
3119+
31013120
def global_identity(x):
31023121
return x
31033122

Python/bytecodes.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -975,9 +975,9 @@ dummy_func(
975975
}
976976

977977
macro(BINARY_OP_SUBSCR_TUPLE_INT) =
978-
_GUARD_TOS_INT + _GUARD_NOS_TUPLE + unused/5 + _BINARY_OP_SUBSCR_TUPLE_INT;
978+
_GUARD_TOS_INT + _GUARD_NOS_TUPLE + unused/5 + _BINARY_OP_SUBSCR_TUPLE_INT + _POP_TOP_INT + POP_TOP;
979979

980-
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) {
980+
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
981981
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
982982
PyObject *tuple = PyStackRef_AsPyObjectBorrow(tuple_st);
983983

@@ -991,9 +991,10 @@ dummy_func(
991991
STAT_INC(BINARY_OP, hit);
992992
PyObject *res_o = PyTuple_GET_ITEM(tuple, index);
993993
assert(res_o != NULL);
994-
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
995994
res = PyStackRef_FromPyObjectNew(res_o);
996-
DECREF_INPUTS();
995+
ts = tuple_st;
996+
ss = sub_st;
997+
INPUTS_DEAD();
997998
}
998999

9991000
op(_GUARD_NOS_DICT, (nos, unused -- nos, unused)) {

Python/executor_cases.c.h

Lines changed: 8 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_bytecodes.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ dummy_func(void) {
335335
i = sub_st;
336336
}
337337

338-
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) {
338+
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res, ts, ss)) {
339339
assert(sym_matches_type(tuple_st, &PyTuple_Type));
340340
if (sym_is_const(ctx, sub_st)) {
341341
assert(PyLong_CheckExact(sym_get_const(ctx, sub_st)));
@@ -354,6 +354,8 @@ dummy_func(void) {
354354
else {
355355
res = sym_new_not_null(ctx);
356356
}
357+
ts = tuple_st;
358+
ss = sub_st;
357359
}
358360

359361
op(_TO_BOOL, (value -- res)) {

Python/optimizer_cases.c.h

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)