Skip to content

Commit 04da416

Browse files
authored
gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_LIST_INT (gh-142703)
1 parent af7cca3 commit 04da416

File tree

9 files changed

+96
-21
lines changed

9 files changed

+96
-21
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: 3 additions & 3 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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,24 @@ def testfunc(n):
24992499
self.assertNotIn("_GUARD_TOS_INT", uops)
25002500
self.assertNotIn("_GUARD_NOS_INT", uops)
25012501

2502+
def test_store_subscr_int(self):
2503+
def testfunc(n):
2504+
l = [0, 0, 0, 0]
2505+
for _ in range(n):
2506+
l[0] = 1
2507+
l[1] = 2
2508+
l[2] = 3
2509+
l[3] = 4
2510+
return sum(l)
2511+
2512+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2513+
self.assertEqual(res, 10)
2514+
self.assertIsNotNone(ex)
2515+
uops = get_opnames(ex)
2516+
self.assertNotIn("_POP_TOP", uops)
2517+
self.assertNotIn("_POP_TOP_INT", uops)
2518+
self.assertIn("_POP_TOP_NOP", uops)
2519+
25022520
def test_attr_promotion_failure(self):
25032521
# We're not testing for any specific uops here, just
25042522
# testing it doesn't crash.

Python/bytecodes.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,9 +1126,9 @@ dummy_func(
11261126
macro(STORE_SUBSCR) = _SPECIALIZE_STORE_SUBSCR + _STORE_SUBSCR;
11271127

11281128
macro(STORE_SUBSCR_LIST_INT) =
1129-
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/1 + _STORE_SUBSCR_LIST_INT;
1129+
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/1 + _STORE_SUBSCR_LIST_INT + _POP_TOP_INT + POP_TOP;
11301130

1131-
op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- )) {
1131+
op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- ls, ss)) {
11321132
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
11331133
PyObject *list = PyStackRef_AsPyObjectBorrow(list_st);
11341134

@@ -1151,9 +1151,9 @@ dummy_func(
11511151
PyStackRef_AsPyObjectSteal(value));
11521152
assert(old_value != NULL);
11531153
UNLOCK_OBJECT(list); // unlock before decrefs!
1154-
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
1155-
DEAD(sub_st);
1156-
PyStackRef_CLOSE(list_st);
1154+
INPUTS_DEAD();
1155+
ls = list_st;
1156+
ss = sub_st;
11571157
Py_DECREF(old_value);
11581158
}
11591159

Python/executor_cases.c.h

Lines changed: 14 additions & 6 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: 22 additions & 3 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ dummy_func(void) {
103103
GETLOCAL(oparg) = value;
104104
}
105105

106+
op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- ls, ss)) {
107+
(void)value;
108+
ls = list_st;
109+
ss = sub_st;
110+
}
111+
106112
op(_PUSH_NULL, (-- res)) {
107113
res = sym_new_null(ctx);
108114
}
@@ -529,6 +535,12 @@ dummy_func(void) {
529535
}
530536
}
531537

538+
op(_POP_TOP_INT, (value --)) {
539+
if (PyJitRef_IsBorrowed(value)) {
540+
REPLACE_OP(this_instr, _POP_TOP_NOP, 0, 0);
541+
}
542+
}
543+
532544
op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
533545
assert(oparg > 0);
534546
top = bottom;

Python/optimizer_cases.c.h

Lines changed: 20 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)