gh-138912: Add fast path for match class patterns without sub-patterns#144820
gh-138912: Add fast path for match class patterns without sub-patterns#144820cdce8p wants to merge 1 commit intopython:mainfrom
Conversation
aa2f4a9 to
8cfaa01
Compare
8cfaa01 to
a515a54
Compare
a515a54 to
7f8b4b2
Compare
|
Opcodes are a very limited resource, so we need to justify it carefully when adding new ones. Is pattern matching widely used enough yet to justify this? I don't know. An approach to optimizing pattern matching that I would be more enthusiastic about is optimizing the decision tree. For example, match m:
case A(a, b):
X
case A():
Ycan be transformed into the decision tree, like this: if m isa A:
if tmp := unpack_attributes(m, 2):
a, b = tmp
X
else:
Ywhich can be converted into code that has simpler operations and is more amenable to further specialization/jitting. |
|
Inspired by Mark's comments I looked at the generated bytecode: Output: This can be simplified to (diff main...eendebakpt:pattern_match_class_v0) A quick benchmarks shows this is a bit faster. I tried changing the bytecode generation to use an |
Extracted from #139080. This builds on the work merged with 75d4839.
Add a fast path for match class patterns without any sub-patterns. This avoids creating a new (empty) tuple, pushing it to the stack and subsequently unpacking the tuple result from the
MATCH_CLASSopcode again.__
Micro benchmarks with all specializations enabled.
Micro benchmark (bare class pattern)
If specialization disabled, the performance improvement is even better with -40.5%.
📚 Documentation preview 📚: https://cpython-previews--144820.org.readthedocs.build/