Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions Include/internal/pycore_opcode_metadata.h

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

6 changes: 3 additions & 3 deletions Include/internal/pycore_uop_ids.h

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

24 changes: 12 additions & 12 deletions Include/internal/pycore_uop_metadata.h

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

51 changes: 51 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,57 @@ def testfunc(n):
self.assertIn("_BINARY_OP_SUBSCR_DICT", uops)
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)

def test_contains_op(self):
def testfunc(n):
x = 0
items = [1, 2, 3]
for _ in range(n):
if 2 in items:
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("_CONTAINS_OP", uops)
self.assertIn("_POP_TOP_NOP", uops)
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)

def test_contains_op_set(self):
def testfunc(n):
x = 0
s = {1, 2, 3}
for _ in range(n):
if 2 in s:
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("_CONTAINS_OP_SET", uops)
self.assertIn("_POP_TOP_NOP", uops)
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)

def test_contains_op_dict(self):
def testfunc(n):
x = 0
d = {'a': 1, 'b': 2}
for _ in range(n):
if 'a' in d:
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("_CONTAINS_OP_DICT", uops)
self.assertIn("_POP_TOP_NOP", uops)
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 2)

def test_call_type_1_guards_removed(self):
def testfunc(n):
x = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Eliminate redundant refcounting from ``_CONTAINS_OP``, ``_CONTAINS_OP_SET``
and ``_CONTAINS_OP_DICT``.
36 changes: 24 additions & 12 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2855,14 +2855,18 @@ dummy_func(
CONTAINS_OP_DICT,
};

op(_CONTAINS_OP, (left, right -- b)) {
op(_CONTAINS_OP, (left, right -- b, l, r)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);

int res = PySequence_Contains(right_o, left_o);
DECREF_INPUTS();
ERROR_IF(res < 0);
if (res < 0) {
ERROR_NO_POP();
}
b = (res ^ oparg) ? PyStackRef_True : PyStackRef_False;
l = left;
r = right;
INPUTS_DEAD();
}

specializing op(_SPECIALIZE_CONTAINS_OP, (counter/1, left, right -- left, right)) {
Expand All @@ -2877,40 +2881,48 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION_FT */
}

macro(CONTAINS_OP) = _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP;
macro(CONTAINS_OP) = _SPECIALIZE_CONTAINS_OP + _CONTAINS_OP + POP_TOP + POP_TOP;

op(_GUARD_TOS_ANY_SET, (tos -- tos)) {
PyObject *o = PyStackRef_AsPyObjectBorrow(tos);
DEOPT_IF(!PyAnySet_CheckExact(o));
}

macro(CONTAINS_OP_SET) = _GUARD_TOS_ANY_SET + unused/1 + _CONTAINS_OP_SET;
macro(CONTAINS_OP_SET) = _GUARD_TOS_ANY_SET + unused/1 + _CONTAINS_OP_SET + POP_TOP + POP_TOP;

op(_CONTAINS_OP_SET, (left, right -- b)) {
op(_CONTAINS_OP_SET, (left, right -- b, l, r)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);

assert(PyAnySet_CheckExact(right_o));
STAT_INC(CONTAINS_OP, hit);
// Note: both set and frozenset use the same seq_contains method!
int res = _PySet_Contains((PySetObject *)right_o, left_o);
DECREF_INPUTS();
ERROR_IF(res < 0);
if (res < 0) {
ERROR_NO_POP();
}
b = (res ^ oparg) ? PyStackRef_True : PyStackRef_False;
l = left;
r = right;
INPUTS_DEAD();
}

macro(CONTAINS_OP_DICT) = _GUARD_TOS_DICT + unused/1 + _CONTAINS_OP_DICT;
macro(CONTAINS_OP_DICT) = _GUARD_TOS_DICT + unused/1 + _CONTAINS_OP_DICT + POP_TOP + POP_TOP;

op(_CONTAINS_OP_DICT, (left, right -- b)) {
op(_CONTAINS_OP_DICT, (left, right -- b, l, r)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);

assert(PyDict_CheckExact(right_o));
STAT_INC(CONTAINS_OP, hit);
int res = PyDict_Contains(right_o, left_o);
DECREF_INPUTS();
ERROR_IF(res < 0);
if (res < 0) {
ERROR_NO_POP();
}
b = (res ^ oparg) ? PyStackRef_True : PyStackRef_False;
l = left;
r = right;
INPUTS_DEAD();
}

inst(CHECK_EG_MATCH, (exc_value_st, match_type_st -- rest, match)) {
Expand Down
72 changes: 30 additions & 42 deletions Python/executor_cases.c.h

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

Loading
Loading