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
11 changes: 11 additions & 0 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,17 @@ iterations of the loop.
.. versionadded:: 3.13


.. opcode:: MATCH_CLASS_GET_OPT_ATTR (namei)

Replaces ``STACK[-1]`` with ``getattr(STACK[-1], co_names[namei])`` and gets
optional attribute from match subject ``STACK[-2]``.

Pop ``Stack[-1]``. If attribute is found, pushes it and ``True`` onto the stack.
Otherwise pushes ``None`` and ``False``.

.. versionadded:: 3.16


.. opcode:: MATCH_CLASS (count)

``STACK[-1]`` is a tuple of keyword attribute names, ``STACK[-2]`` is the class
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ Known values:
Python 3.16a1 3704 (Replace DELETE_ATTR with PUSH_NULL; STORE_ATTR)
Python 3.16a1 3705 (Add INTRINSIC_ADD_CONDITIONAL_ANNOTATION)
Python 3.16a1 3706 (Add INTRINSIC_MATCH_CLASS_ISINSTANCE)
Python 3.16a1 3707 (Add MATCH_CLASS_GET_OPT_ATTR opcode)

Python 3.17 will start with 3750

Expand All @@ -313,7 +314,7 @@ Known values:

*/

#define PYC_MAGIC_NUMBER 3706
#define PYC_MAGIC_NUMBER 3707
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
10 changes: 8 additions & 2 deletions Include/internal/pycore_opcode_metadata.h

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

4 changes: 3 additions & 1 deletion Include/internal/pycore_uop_ids.h

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

15 changes: 15 additions & 0 deletions Include/internal/pycore_uop_metadata.h

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

43 changes: 22 additions & 21 deletions Include/opcode_ids.h

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

43 changes: 22 additions & 21 deletions Lib/_opcode_metadata.py

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

15 changes: 14 additions & 1 deletion Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,14 @@ def test_static_swaps_match_mapping(self):
self.assertNotInBytecode(code, "SWAP")

def test_static_swaps_match_class(self):
swaps = {
"C(a=a, b=_, c=_)",
"C(a=a, b=_, c=c)",
"C(a=a, b=b, c=_)",
"C(a=a, b=b, c=c)",
"C(a=_, b=b, c=_)",
"C(a=_, b=b, c=c)",
}
forms = [
"C({}, {}, {})",
"C({}, {}, c={})",
Expand All @@ -799,7 +807,12 @@ def test_static_swaps_match_class(self):
pattern = form.format(a, b, c)
with self.subTest(pattern):
code = compile_pattern_with_fast_locals(pattern)
self.assertNotInBytecode(code, "SWAP")
if pattern in swaps:
# Swaps are expected here. Class patterns with only
# keyword sub-patterns are evaluated depth first.
self.assertInBytecode(code, "SWAP")
else:
self.assertNotInBytecode(code, "SWAP")

def test_static_swaps_match_sequence(self):
swaps = {"*_, b, c", "a, *_, c", "a, b, *_"}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :opcode:`MATCH_CLASS_GET_OPT_ATTR` to improve the performance of
:keyword:`match` class patterns with only keyword sub-patterns by 10% to
30%. Patch by Marc Mueller.
38 changes: 38 additions & 0 deletions Modules/_testinternalcapi/test_cases.c.h

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

Loading
Loading