Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_DICT
  • Loading branch information
corona10 committed Dec 14, 2025
commit ed7add71be532549b2c525e2d97aa268bbc69671
4 changes: 2 additions & 2 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Include/internal/pycore_uop_ids.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2513,10 +2513,29 @@ def testfunc(n):
self.assertEqual(res, 10)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_STORE_SUBSCR_LIST_INT", uops)
self.assertNotIn("_POP_TOP", uops)
self.assertNotIn("_POP_TOP_INT", uops)
self.assertIn("_POP_TOP_NOP", uops)

def test_store_susbscr_dict(self):
def testfunc(n):
d = {}
for _ in range(n):
d['a'] = 1
d['b'] = 2
d['c'] = 3
d['d'] = 4
return sum(d.values())

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, 10)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_STORE_SUBSCR_DICT", uops)
self.assertNotIn("_POP_TOP", uops)
self.assertIn("_POP_TOP_NOP", uops)

def test_attr_promotion_failure(self):
# We're not testing for any specific uops here, just
# testing it doesn't crash.
Expand Down
17 changes: 13 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,18 +1158,21 @@ dummy_func(
}

macro(STORE_SUBSCR_DICT) =
_GUARD_NOS_DICT + unused/1 + _STORE_SUBSCR_DICT;
_GUARD_NOS_DICT + unused/1 + _STORE_SUBSCR_DICT + POP_TOP;

op(_STORE_SUBSCR_DICT, (value, dict_st, sub -- )) {
op(_STORE_SUBSCR_DICT, (value, dict_st, sub -- st)) {
PyObject *dict = PyStackRef_AsPyObjectBorrow(dict_st);

assert(PyDict_CheckExact(dict));
STAT_INC(STORE_SUBSCR, hit);
int err = _PyDict_SetItem_Take2((PyDictObject *)dict,
PyStackRef_AsPyObjectSteal(sub),
PyStackRef_AsPyObjectSteal(value));
PyStackRef_CLOSE(dict_st);
ERROR_IF(err);
if (err) {
ERROR_NO_POP();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't do this here. The problem is that _PyDict_SetItem_Take2 has already stolen the references to sub and value. This means when we jump to error, we decref sub and value again, causing a double decref.

The fix might be to do

if (err) {
    PyStackRef_CLOSE(dict_st);
    ERROR_IF(1);
}

}
INPUTS_DEAD();
st = dict_st;
}

inst(DELETE_SUBSCR, (container, sub --)) {
Expand Down Expand Up @@ -5458,6 +5461,12 @@ dummy_func(
}
}

label(pop_3_error) {
stack_pointer -= 3;
assert(WITHIN_STACK_BOUNDS());
goto error;
}

label(pop_2_error) {
stack_pointer -= 2;
assert(WITHIN_STACK_BOUNDS());
Expand Down
15 changes: 7 additions & 8 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Python/opcode_targets.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ dummy_func(void) {
ss = sub_st;
}

op(_STORE_SUBSCR_DICT, (value, dict_st, sub -- st)) {
(void)value;
Comment thread
Fidget-Spinner marked this conversation as resolved.
st = dict_st;
}

op(_PUSH_NULL, (-- res)) {
res = sym_new_null(ctx);
}
Expand Down
12 changes: 10 additions & 2 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading