-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
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
Changes from 3 commits
7c81286
597adb0
a2fa0a8
f585253
5281c71
b468647
ef84a10
30ac89f
e0c2c1e
133f94e
640b205
9375e2b
f5eeacc
fd7baee
8d6e099
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Improve :opcode:`MATCH_CLASS` performance. Patch by Marc Mueller | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -709,15 +709,17 @@ 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); | ||||||
| if (seen != NULL) { | ||||||
|
cdce8p marked this conversation as resolved.
|
||||||
| 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 +742,24 @@ _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
|
||||||
| if (!nattrs) { | ||||||
| PyObject *attrs = PyTuple_New(0); | ||||||
| return attrs; | ||||||
|
cdce8p marked this conversation as resolved.
Outdated
|
||||||
| } | ||||||
| // So far so good: | ||||||
| PyObject *seen = PySet_New(NULL); | ||||||
| if (seen == NULL) { | ||||||
| return NULL; | ||||||
| PyObject *seen = NULL; | ||||||
| if (nattrs > 1) { | ||||||
|
Contributor
Author
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.
Suggested change
As mentioned in #138912, I'd also like to change this. If there are only keyword arguments, it's easy to see if two are the same name. Furthermore linters are able to detect these based on the AST alone. It would also be great to say if we could suggest users to prefer keyword arguments. Not only are they more explicit, they are also faster. (No lookup of
Contributor
Author
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. Added this in f585253 with
Contributor
Author
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. |
||||||
| 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 +800,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.
Outdated
|
||||||
| } | ||||||
| else { | ||||||
| for (Py_ssize_t i = 0; i < nargs; i++) { | ||||||
|
|
@@ -806,36 +817,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.
|
||||||
| } | ||||||
| } | ||||||
| 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.
|
||||||
| } | ||||||
| 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; | ||||||
| } | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.