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
17 changes: 17 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,23 @@ def testfunc(n):
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)
self.assertLessEqual(count_ops(ex, "_POP_TOP_INT"), 1)

def test_binary_op_subscr_dict(self):
def testfunc(n):
x = 0
d = {'a': 1, 'b': 2}
for _ in range(n):
v = d['a'] # _BINARY_OP_SUBSCR_DICT
if v == 1:
x += 1
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_BINARY_OP_SUBSCR_DICT", uops)
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)

def test_call_type_1_guards_removed(self):
def testfunc(n):
x = 0
Expand Down
8 changes: 5 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,9 +1056,9 @@ dummy_func(
}

macro(BINARY_OP_SUBSCR_DICT) =
_GUARD_NOS_DICT + unused/5 + _BINARY_OP_SUBSCR_DICT;
_GUARD_NOS_DICT + unused/5 + _BINARY_OP_SUBSCR_DICT + POP_TOP + POP_TOP;

op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res)) {
op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) {
PyObject *sub = PyStackRef_AsPyObjectBorrow(sub_st);
PyObject *dict = PyStackRef_AsPyObjectBorrow(dict_st);

Expand All @@ -1069,9 +1069,11 @@ dummy_func(
if (rc == 0) {
_PyErr_SetKeyError(sub);
}
DECREF_INPUTS();
INPUTS_DEAD();
ERROR_IF(rc <= 0); // not found or error
res = PyStackRef_FromPyObjectSteal(res_o);
ds = dict_st;
ss = sub_st;
Comment thread
caje731 marked this conversation as resolved.
}

op(_BINARY_OP_SUBSCR_CHECK_FUNC, (container, unused -- container, unused, getitem)) {
Expand Down
28 changes: 12 additions & 16 deletions Python/executor_cases.c.h

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

40 changes: 24 additions & 16 deletions Python/generated_cases.c.h

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

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

op(_BINARY_OP_SUBSCR_DICT, (dict_st, sub_st -- res, ds, ss)) {
res = sym_new_not_null(ctx);
ds = dict_st;
ss = sub_st;
INPUTS_DEAD();
Comment thread
caje731 marked this conversation as resolved.
Outdated
}

op(_TO_BOOL, (value -- res)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
if (!already_bool) {
Expand Down
14 changes: 12 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