Skip to content

Commit cc48bf0

Browse files
authored
gh-134584: Eliminate redundant refcounting from _BINARY_OP_SUBSCR_TUPLE_INT (GH-143094)
1 parent cbe0cb7 commit cc48bf0

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
@@ -3114,6 +3114,25 @@ def testfunc(n):
31143114
self.assertNotIn("_POP_TOP_INT", uops)
31153115
self.assertIn("_POP_TOP_NOP", uops)
31163116

3117+
def test_binary_subscr_tuple_int(self):
3118+
def testfunc(n):
3119+
t = (1,)
3120+
x = 0
3121+
for _ in range(n):
3122+
y = t[0]
3123+
x += y
3124+
return x
3125+
3126+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
3127+
self.assertEqual(res, TIER2_THRESHOLD)
3128+
self.assertIsNotNone(ex)
3129+
uops = get_opnames(ex)
3130+
3131+
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
3132+
self.assertNotIn("_POP_TOP", uops)
3133+
self.assertNotIn("_POP_TOP_INT", uops)
3134+
self.assertIn("_POP_TOP_NOP", uops)
3135+
31173136
def test_143026(self):
31183137
# https://github.com/python/cpython/issues/143026
31193138

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)