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
34 changes: 33 additions & 1 deletion Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4646,7 +4646,7 @@ def testfunc(n):
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)
self.assertGreaterEqual(count_ops(ex, "_LOAD_CONST_INLINE_BORROW"), 2)
self.assertNotIn("_BINARY_OP_SUBSCR_DICT", uops)

def test_binary_subscr_frozendict_const_fold(self):
Expand All @@ -4661,6 +4661,7 @@ def testfunc(n):
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertGreaterEqual(count_ops(ex, "_LOAD_CONST_INLINE_BORROW"), 3)
# lookup result is folded to constant 1, so comparison is optimized away
self.assertNotIn("_COMPARE_OP_INT", uops)

Expand All @@ -4676,8 +4677,39 @@ def testfunc(n):
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertGreaterEqual(count_ops(ex, "_LOAD_CONST_INLINE_BORROW"), 3)
self.assertNotIn("_CONTAINS_OP_SET", uops)

def test_contains_op_frozendict_const_fold(self):
def testfunc(n):
x = 0
for _ in range(n):
if 'x' in FROZEN_DICT_CONST:
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.assertGreaterEqual(count_ops(ex, "_LOAD_CONST_INLINE_BORROW"), 3)
self.assertNotIn("_CONTAINS_OP_DICT", uops)

def test_not_contains_op_frozendict_const_fold(self):
def testfunc(n):
x = 0
for _ in range(n):
if 'z' not in FROZEN_DICT_CONST:
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.assertGreaterEqual(count_ops(ex, "_LOAD_CONST_INLINE_BORROW"), 3)
self.assertNotIn("_CONTAINS_OP_DICT", uops)

def test_binary_subscr_list_slice(self):
def testfunc(n):
x = 0
Expand Down
4 changes: 4 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,10 @@ dummy_func(void) {
b = sym_new_type(ctx, &PyBool_Type);
l = left;
r = right;
if (sym_is_not_container(left) &&
sym_matches_type(right, &PyFrozenDict_Type)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(left, right, b);
}
}

op(_LOAD_CONST, (-- value)) {
Expand Down
47 changes: 47 additions & 0 deletions Python/optimizer_cases.c.h

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

Loading