-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-138912: Improve MATCH_CLASS opcode performance #138915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cdce8p
wants to merge
6
commits into
python:main
Choose a base branch
from
cdce8p:match-class-performance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c81286
gh-138912: Improve MATCH_CLASS opcode performance
cdce8p 597adb0
Revert using a tuple directly
cdce8p a2fa0a8
Use tuple for attrs directly
cdce8p f585253
Only check for duplicates if there is at least one positional pattern
cdce8p 5281c71
Add additional comments
cdce8p b468647
Code review
cdce8p File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2025-09-15-13-28-48.gh-issue-138912.61EYbn.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve :opcode:`MATCH_CLASS` performance. Patch by Marc Mueller | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -709,15 +709,18 @@ match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type, | |
PyObject *name, PyObject *seen) | ||
{ | ||
assert(PyUnicode_CheckExact(name)); | ||
assert(PySet_CheckExact(seen)); | ||
if (PySet_Contains(seen, name) || PySet_Add(seen, name)) { | ||
if (!_PyErr_Occurred(tstate)) { | ||
// Seen it before! | ||
_PyErr_Format(tstate, PyExc_TypeError, | ||
"%s() got multiple sub-patterns for attribute %R", | ||
((PyTypeObject*)type)->tp_name, name); | ||
// Only check for duplicates if seen is not NULL. | ||
if (seen != NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For what do you need this change? Maybe I'm missing, but other changes don't require this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ough, I see. At least this needs a comment, IMHO. |
||
assert(PySet_CheckExact(seen)); | ||
if (PySet_Contains(seen, name) || PySet_Add(seen, name)) { | ||
if (!_PyErr_Occurred(tstate)) { | ||
// Seen it before! | ||
_PyErr_Format(tstate, PyExc_TypeError, | ||
"%s() got multiple sub-patterns for attribute %R", | ||
((PyTypeObject*)type)->tp_name, name); | ||
} | ||
return NULL; | ||
} | ||
return NULL; | ||
} | ||
PyObject *attr; | ||
(void)PyObject_GetOptionalAttr(subject, name, &attr); | ||
|
@@ -740,14 +743,26 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, | |
if (PyObject_IsInstance(subject, type) <= 0) { | ||
return NULL; | ||
} | ||
// Short circuit if there aren't any arguments: | ||
Py_ssize_t nkwargs = PyTuple_GET_SIZE(kwargs); | ||
Py_ssize_clean_t nattrs = nargs + nkwargs; | ||
cdce8p marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (!nattrs) { | ||
PyObject *attrs = PyTuple_New(0); | ||
return attrs; | ||
cdce8p marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
// So far so good: | ||
PyObject *seen = PySet_New(NULL); | ||
if (seen == NULL) { | ||
return NULL; | ||
PyObject *seen = NULL; | ||
// Only check for duplicates if there is at least one positional attribute | ||
// and two or more attributes in total. | ||
if (nargs > 0 && nattrs > 1) { | ||
seen = PySet_New(NULL); | ||
if (seen == NULL) { | ||
return NULL; | ||
} | ||
} | ||
PyObject *attrs = PyList_New(0); | ||
PyObject *attrs = PyTuple_New(nattrs); | ||
if (attrs == NULL) { | ||
Py_DECREF(seen); | ||
Py_XDECREF(seen); | ||
return NULL; | ||
} | ||
// NOTE: From this point on, goto fail on failure: | ||
|
@@ -788,9 +803,8 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, | |
} | ||
if (match_self) { | ||
// Easy. Copy the subject itself, and move on to kwargs. | ||
if (PyList_Append(attrs, subject) < 0) { | ||
goto fail; | ||
} | ||
Py_INCREF(subject); | ||
PyTuple_SET_ITEM(attrs, 0, subject); | ||
cdce8p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
else { | ||
for (Py_ssize_t i = 0; i < nargs; i++) { | ||
|
@@ -806,36 +820,27 @@ _PyEval_MatchClass(PyThreadState *tstate, PyObject *subject, PyObject *type, | |
if (attr == NULL) { | ||
goto fail; | ||
} | ||
if (PyList_Append(attrs, attr) < 0) { | ||
Py_DECREF(attr); | ||
goto fail; | ||
} | ||
Py_DECREF(attr); | ||
PyTuple_SET_ITEM(attrs, i, attr); | ||
cdce8p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
Py_CLEAR(match_args); | ||
} | ||
// Finally, the keyword subpatterns: | ||
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) { | ||
for (Py_ssize_t i = 0; i < nkwargs; i++) { | ||
PyObject *name = PyTuple_GET_ITEM(kwargs, i); | ||
PyObject *attr = match_class_attr(tstate, subject, type, name, seen); | ||
if (attr == NULL) { | ||
goto fail; | ||
} | ||
if (PyList_Append(attrs, attr) < 0) { | ||
Py_DECREF(attr); | ||
goto fail; | ||
} | ||
Py_DECREF(attr); | ||
PyTuple_SET_ITEM(attrs, nargs + i, attr); | ||
cdce8p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
Py_SETREF(attrs, PyList_AsTuple(attrs)); | ||
Py_DECREF(seen); | ||
Py_XDECREF(seen); | ||
return attrs; | ||
fail: | ||
// We really don't care whether an error was raised or not... that's our | ||
// caller's problem. All we know is that the match failed. | ||
Py_XDECREF(match_args); | ||
Py_DECREF(seen); | ||
Py_XDECREF(seen); | ||
Py_DECREF(attrs); | ||
return NULL; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.