Skip to content

Commit ed7add7

Browse files
committed
gh-134584: Eliminate redundant refcounting from _STORE_SUBSCR_DICT
1 parent 78a50ee commit ed7add7

File tree

10 files changed

+79
-25
lines changed

10 files changed

+79
-25
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 2 additions & 2 deletions
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
@@ -2513,10 +2513,29 @@ def testfunc(n):
25132513
self.assertEqual(res, 10)
25142514
self.assertIsNotNone(ex)
25152515
uops = get_opnames(ex)
2516+
self.assertIn("_STORE_SUBSCR_LIST_INT", uops)
25162517
self.assertNotIn("_POP_TOP", uops)
25172518
self.assertNotIn("_POP_TOP_INT", uops)
25182519
self.assertIn("_POP_TOP_NOP", uops)
25192520

2521+
def test_store_susbscr_dict(self):
2522+
def testfunc(n):
2523+
d = {}
2524+
for _ in range(n):
2525+
d['a'] = 1
2526+
d['b'] = 2
2527+
d['c'] = 3
2528+
d['d'] = 4
2529+
return sum(d.values())
2530+
2531+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2532+
self.assertEqual(res, 10)
2533+
self.assertIsNotNone(ex)
2534+
uops = get_opnames(ex)
2535+
self.assertIn("_STORE_SUBSCR_DICT", uops)
2536+
self.assertNotIn("_POP_TOP", uops)
2537+
self.assertIn("_POP_TOP_NOP", uops)
2538+
25202539
def test_attr_promotion_failure(self):
25212540
# We're not testing for any specific uops here, just
25222541
# testing it doesn't crash.

Python/bytecodes.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,18 +1158,21 @@ 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+
ERROR_NO_POP();
1173+
}
1174+
INPUTS_DEAD();
1175+
st = dict_st;
11731176
}
11741177

11751178
inst(DELETE_SUBSCR, (container, sub --)) {
@@ -5458,6 +5461,12 @@ dummy_func(
54585461
}
54595462
}
54605463

5464+
label(pop_3_error) {
5465+
stack_pointer -= 3;
5466+
assert(WITHIN_STACK_BOUNDS());
5467+
goto error;
5468+
}
5469+
54615470
label(pop_2_error) {
54625471
stack_pointer -= 2;
54635472
assert(WITHIN_STACK_BOUNDS());

Python/executor_cases.c.h

Lines changed: 7 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: 17 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/opcode_targets.h

Lines changed: 1 addition & 0 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)