Skip to content

Commit 9b93ce5

Browse files
committed
gh-134584: Remove redundant refcount for BINARY_OP_SUBSCR_DICT
1 parent dbd10a6 commit 9b93ce5

File tree

6 files changed

+77
-37
lines changed

6 files changed

+77
-37
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,23 @@ def testfunc(n):
19751975
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
19761976
self.assertLessEqual(count_ops(ex, "_POP_TOP_INT"), 1)
19771977

1978+
def test_binary_op_subscr_dict(self):
1979+
def testfunc(n):
1980+
x = 0
1981+
d = {'a': 1, 'b': 2}
1982+
for _ in range(n):
1983+
v = d['a'] # _BINARY_OP_SUBSCR_DICT
1984+
if v == 1:
1985+
x += 1
1986+
return x
1987+
1988+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1989+
self.assertEqual(res, TIER2_THRESHOLD)
1990+
self.assertIsNotNone(ex)
1991+
uops = get_opnames(ex)
1992+
self.assertIn("_BINARY_OP_SUBSCR_DICT", uops)
1993+
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
1994+
19781995
def test_call_type_1_guards_removed(self):
19791996
def testfunc(n):
19801997
x = 0

Python/bytecodes.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,9 @@ dummy_func(
10561056
}
10571057

10581058
macro(BINARY_OP_SUBSCR_DICT) =
1059-
_GUARD_NOS_DICT + unused/5 + _BINARY_OP_SUBSCR_DICT;
1059+
_GUARD_NOS_DICT + unused/5 + _BINARY_OP_SUBSCR_DICT + POP_TOP + POP_TOP;
10601060

1061-
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res)) {
1061+
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) {
10621062
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
10631063
PyObject *dict = PyStackRef_AsPyObjectBorrow(dict_st);
10641064

@@ -1069,9 +1069,11 @@ dummy_func(
10691069
if (rc == 0) {
10701070
_PyErr_SetKeyError(sub);
10711071
}
1072-
DECREF_INPUTS();
1072+
INPUTS_DEAD();
10731073
ERROR_IF(rc <= 0); // not found or error
10741074
res = PyStackRef_FromPyObjectSteal(res_o);
1075+
ds = dict_st;
1076+
ss = sub_st;
10751077
}
10761078

10771079
op(_BINARY_OP_SUBSCR_CHECK_FUNC, (container, unused -- container, unused, getitem)) {

Python/executor_cases.c.h

Lines changed: 12 additions & 16 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: 24 additions & 16 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
@@ -380,6 +380,13 @@ dummy_func(void) {
380380
ss = sub_st;
381381
}
382382

383+
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) {
384+
res = sym_new_not_null(ctx);
385+
ds = dict_st;
386+
ss = sub_st;
387+
INPUTS_DEAD();
388+
}
389+
383390
op(_TO_BOOL, (value -- res)) {
384391
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
385392
if (!already_bool) {

Python/optimizer_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.

0 commit comments

Comments
 (0)