Skip to content

Commit a0ba51f

Browse files
committed
gh-134584: Eliminate redundant refcounting from STORE_SUBSCR_LIST_INT
1 parent e5f03b9 commit a0ba51f

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
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.

Lib/test/test_capi/test_opt.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,21 @@ def testfunc(n):
23622362
self.assertNotIn("_GUARD_TOS_INT", uops)
23632363
self.assertNotIn("_GUARD_NOS_INT", uops)
23642364

2365+
def test_store_subscr_int(self):
2366+
def testfunc(args):
2367+
n = args[0]
2368+
l = [0] * n
2369+
ret = 0
2370+
for idx in range(n):
2371+
l[idx] = idx
2372+
return sum(l)
2373+
2374+
res, ex = self._run_with_optimizer(testfunc, (TIER2_THRESHOLD,))
2375+
self.assertEqual(res, sum(range(TIER2_THRESHOLD)))
2376+
self.assertIsNotNone(ex)
2377+
uops = get_opnames(ex)
2378+
self.assertIn("_POP_TOP_INT", uops)
2379+
23652380
def test_attr_promotion_failure(self):
23662381
# We're not testing for any specific uops here, just
23672382
# testing it doesn't crash.

Python/bytecodes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,9 +1121,9 @@ dummy_func(
11211121
macro(STORE_SUBSCR) = _SPECIALIZE_STORE_SUBSCR + _STORE_SUBSCR;
11221122

11231123
macro(STORE_SUBSCR_LIST_INT) =
1124-
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/1 + _STORE_SUBSCR_LIST_INT;
1124+
_GUARD_TOS_INT + _GUARD_NOS_LIST + unused/1 + _STORE_SUBSCR_LIST_INT + _POP_TOP_INT;
11251125

1126-
op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- )) {
1126+
op(_STORE_SUBSCR_LIST_INT, (value, list_st, sub_st -- ss)) {
11271127
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
11281128
PyObject *list = PyStackRef_AsPyObjectBorrow(list_st);
11291129

@@ -1146,8 +1146,8 @@ dummy_func(
11461146
PyStackRef_AsPyObjectSteal(value));
11471147
assert(old_value != NULL);
11481148
UNLOCK_OBJECT(list); // unlock before decrefs!
1149-
PyStackRef_CLOSE_SPECIALIZED(sub_st, _PyLong_ExactDealloc);
11501149
DEAD(sub_st);
1150+
ss = sub_st;
11511151
PyStackRef_CLOSE(list_st);
11521152
Py_DECREF(old_value);
11531153
}

Python/executor_cases.c.h

Lines changed: 4 additions & 2 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: 12 additions & 2 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,13 @@ dummy_func(void) {
567567
value = PyJitRef_Borrow(sym_new_const(ctx, ptr));
568568
}
569569

570+
op(_POP_TOP_INT, (value -- )) {
571+
if (PyJitRef_IsBorrowed(value) ||
572+
sym_is_immortal(PyJitRef_Unwrap(value))) {
573+
REPLACE_OP(this_instr, _POP_TOP_NOP, 0, 0);
574+
}
575+
}
576+
570577
op(_POP_TOP, (value -- )) {
571578
PyTypeObject *typ = sym_get_type(value);
572579
if (PyJitRef_IsBorrowed(value) ||

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)