Skip to content

Commit abaaeee

Browse files
gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_DICT (GH-142712)
Co-authored-by: Ken Jin <[email protected]>
1 parent 872ab51 commit abaaeee

File tree

9 files changed

+74
-23
lines changed

9 files changed

+74
-23
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,10 +2525,29 @@ def testfunc(n):
25252525
self.assertEqual(res, 10)
25262526
self.assertIsNotNone(ex)
25272527
uops = get_opnames(ex)
2528+
self.assertIn("_STORE_SUBSCR_LIST_INT", uops)
25282529
self.assertNotIn("_POP_TOP", uops)
25292530
self.assertNotIn("_POP_TOP_INT", uops)
25302531
self.assertIn("_POP_TOP_NOP", uops)
25312532

2533+
def test_store_susbscr_dict(self):
2534+
def testfunc(n):
2535+
d = {}
2536+
for _ in range(n):
2537+
d['a'] = 1
2538+
d['b'] = 2
2539+
d['c'] = 3
2540+
d['d'] = 4
2541+
return sum(d.values())
2542+
2543+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2544+
self.assertEqual(res, 10)
2545+
self.assertIsNotNone(ex)
2546+
uops = get_opnames(ex)
2547+
self.assertIn("_STORE_SUBSCR_DICT", uops)
2548+
self.assertNotIn("_POP_TOP", uops)
2549+
self.assertIn("_POP_TOP_NOP", uops)
2550+
25322551
def test_attr_promotion_failure(self):
25332552
# We're not testing for any specific uops here, just
25342553
# testing it doesn't crash.

Python/bytecodes.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,18 +1158,22 @@ dummy_func(
11581158
}
11591159

11601160
macro(STORE_SUBSCR_DICT) =
1161-
_GUARD_NOS_DICT + unused/1 + _STORE_SUBSCR_DICT;
1161+
_GUARD_NOS_DICT + unused/1 + _STORE_SUBSCR_DICT + POP_TOP;
11621162

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

11661166
assert(PyDict_CheckExact(dict));
11671167
STAT_INC(STORE_SUBSCR, hit);
11681168
int err = _PyDict_SetItem_Take2((PyDictObject *)dict,
11691169
PyStackRef_AsPyObjectSteal(sub),
11701170
PyStackRef_AsPyObjectSteal(value));
1171-
PyStackRef_CLOSE(dict_st);
1172-
ERROR_IF(err);
1171+
if (err) {
1172+
PyStackRef_CLOSE(dict_st);
1173+
ERROR_IF(1);
1174+
}
1175+
DEAD(dict_st);
1176+
st = dict_st;
11731177
}
11741178

11751179
inst(DELETE_SUBSCR, (container, sub --)) {

Python/executor_cases.c.h

Lines changed: 12 additions & 8 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: 15 additions & 4 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ dummy_func(void) {
109109
ss = sub_st;
110110
}
111111

112+
op(_STORE_SUBSCR_DICT, (value, dict_st, sub -- st)) {
113+
(void)value;
114+
st = dict_st;
115+
}
116+
112117
op(_PUSH_NULL, (-- res)) {
113118
res = sym_new_null(ctx);
114119
}

Python/optimizer_cases.c.h

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