Skip to content

Commit b4b2106

Browse files
_COMPARE_OP and _CONTAINS_OP
1 parent f7c380e commit b4b2106

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,23 @@ def f(n):
16141614
# But all of the appends we care about are still there:
16151615
self.assertEqual(uops.count("_CALL_LIST_APPEND"), len("ABCDEFG"))
16161616

1617+
def test_compare_op_pop_two_load_const_inline_borrow(self):
1618+
def testfunc(n):
1619+
x = 0
1620+
for _ in range(n):
1621+
a = 10
1622+
b = 10.0
1623+
if a == b:
1624+
x += 1
1625+
return x
1626+
1627+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1628+
self.assertEqual(res, TIER2_THRESHOLD)
1629+
self.assertIsNotNone(ex)
1630+
uops = get_opnames(ex)
1631+
self.assertNotIn("_COMPARE_OP", uops)
1632+
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
1633+
16171634
def test_compare_op_int_pop_two_load_const_inline_borrow(self):
16181635
def testfunc(n):
16191636
x = 0
@@ -1665,6 +1682,23 @@ def testfunc(n):
16651682
self.assertNotIn("_COMPARE_OP_FLOAT", uops)
16661683
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
16671684

1685+
def test_contains_op_pop_two_load_const_inline_borrow(self):
1686+
def testfunc(n):
1687+
x = 0
1688+
for _ in range(n):
1689+
a = "foo"
1690+
s = "foo bar baz"
1691+
if a in s:
1692+
x += 1
1693+
return x
1694+
1695+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1696+
self.assertEqual(res, TIER2_THRESHOLD)
1697+
self.assertIsNotNone(ex)
1698+
uops = get_opnames(ex)
1699+
self.assertNotIn("_CONTAINS_OP", uops)
1700+
self.assertNotIn("_POP_TWO_LOAD_CONST_INLINE_BORROW", uops)
1701+
16681702
def test_to_bool_bool_contains_op_set(self):
16691703
"""
16701704
Test that _TO_BOOL_BOOL is removed from code like:

Python/optimizer_bytecodes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ dummy_func(void) {
422422

423423
op(_COMPARE_OP, (left, right -- res)) {
424424
if (oparg & 16) {
425+
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
425426
res = sym_new_type(ctx, &PyBool_Type);
426427
}
427428
else {
@@ -449,6 +450,7 @@ dummy_func(void) {
449450
}
450451

451452
op(_CONTAINS_OP, (left, right -- b)) {
453+
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right);
452454
b = sym_new_type(ctx, &PyBool_Type);
453455
}
454456

Python/optimizer_cases.c.h

Lines changed: 83 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)