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)) {
PyTypeObject *type = sym_get_type(subject);
if (type != NULL) {
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm I'm not too sure either. I'll try to see if I can find out more

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.

abc.register will change the tp_flags but don't update the type version, so JIT won't invalid the old trace

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.

@Fidget-Spinner Do we need call assign_version_tag when use abc.register?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why? ABC never changes the actual type flags.

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.

abc.register will change the tp_flags but don't update the type version, so JIT won't invalid the old trace

That sounds like a bug in abc.register. We should fix abc.register as that might affect the specializing interpreter as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, I thought it already did invalidation, so yes this is a bug.

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.

@cocolato do you mind opening an issue for abc.register separately please?

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.

Sure, i will try to create the issue and solve this :)

Copy link
Copy Markdown
Member

@Fidget-Spinner Fidget-Spinner Apr 12, 2026

Choose a reason for hiding this comment

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

You don't have to solve it (unless you want to of course), but just opening an issue is fine :). Thank you

res = sym_new_const(ctx, match ? Py_True : Py_False);
}
else {
res = sym_new_type(ctx, &PyBool_Type);
}
}

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

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