Skip to content
Open
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
35 changes: 35 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4684,6 +4684,41 @@ class A:
self.assertIn("_MATCH_CLASS", uops)
self.assertEqual(count_ops(ex, "_POP_TOP_NOP"), 4)

def test_match_mapping(self):
def testfunc(n):
x = {}
ret = 0
for _ in range(n):
x["a"] = 1
match x:
case {}:
ret += 1
return ret

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
uops = get_opnames(ex)

self.assertIn("_MATCH_MAPPING", uops)
self.assertNotIn("_GUARD_BIT_IS_SET_POP", uops)

def test_match_sequence(self):
def testfunc(n):
ret = 0
for _ in range(n):
x = 1, 2
match x:
case a, b:
ret += 1
return ret

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
uops = get_opnames(ex)

self.assertIn("_MATCH_SEQUENCE", uops)
self.assertNotIn("_GUARD_BIT_IS_SET_POP", uops)

def test_dict_update(self):
def testfunc(n):
d = {1: 2, 3: 4}
Expand Down
22 changes: 22 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,28 @@ dummy_func(void) {
n = names;
}

op(_MATCH_MAPPING, (subject -- subject, res)) {
if (sym_has_type(subject)) {
PyTypeObject *type = sym_get_type(subject);
int match = type->tp_flags & Py_TPFLAGS_MAPPING;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wonder if this logic will be broken by abc.register.

res = match ? sym_new_const(ctx, Py_True) : sym_new_const(ctx, Py_False);
}
else {
res = sym_new_type(ctx, &PyBool_Type);
}
Comment on lines +2075 to +2082
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (sym_has_type(subject)) {
PyTypeObject *type = sym_get_type(subject);
int match = type->tp_flags & Py_TPFLAGS_MAPPING;
res = match ? sym_new_const(ctx, Py_True) : sym_new_const(ctx, Py_False);
}
else {
res = sym_new_type(ctx, &PyBool_Type);
}
PyTypeObject *type = sym_get_type(subject);
if (type != NULL) {
int match = type->tp_flags & Py_TPFLAGS_MAPPING;
res = sym_new_const(ctx, match ? Py_True : Py_False);
}
else {
res = sym_new_type(ctx, &PyBool_Type);
}

}

op(_MATCH_SEQUENCE, (subject -- subject, res)) {
if (sym_has_type(subject)) {
PyTypeObject *type = sym_get_type(subject);
int match = type->tp_flags & Py_TPFLAGS_SEQUENCE;
res = match ? sym_new_const(ctx, Py_True) : sym_new_const(ctx, Py_False);
}
else {
res = sym_new_type(ctx, &PyBool_Type);
}
}
Comment on lines +2086 to +2094
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto


op(_DICT_UPDATE, (dict, unused[oparg - 1], update -- dict, unused[oparg - 1], upd)) {
(void)dict;
upd = update;
Expand Down
22 changes: 20 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