-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-45045: Optimize mapping patterns of structural pattern matching #28043
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
Conversation
|
@@ -859,7 +859,7 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys) | |||
if (dummy == NULL) { | |||
goto fail; | |||
} | |||
values = PyList_New(0); | |||
values = PyTuple_New(nkeys); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The size of the tuple is predictable.
Python/ceval.c
Outdated
@@ -873,7 +873,8 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys) | |||
} | |||
goto fail; | |||
} | |||
PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL); | |||
PyObject *args[] = { key, dummy }; | |||
PyObject *value = PyObject_Vectorcall(get, args, 2, NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just replacing PyObject_CallFunctionObjArgs shows a 2% performance enhancement on the micro benchmark.
The changes LGTM. Tested locally on Win64:
BTW, I was thinking if using @@ -846,7 +846,9 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
// - Don't cause key creation or resizing in dict subclasses like
// collections.defaultdict that define __missing__ (or similar).
_Py_IDENTIFIER(get);
- PyObject *get = _PyObject_GetAttrId(map, &PyId_get);
+ PyObject *get_name = _PyUnicode_FromId(&PyId_get); // borrowed
+ PyObject *get = NULL;
+ int meth_found = _PyObject_GetMethod(map, get_name, &get);
if (get == NULL) {
goto fail;
}
@@ -873,8 +875,14 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
}
goto fail;
}
- PyObject *args[] = { key, dummy };
- PyObject *value = PyObject_Vectorcall(get, args, 2, NULL);
+ PyObject *args[] = { map, key, dummy };
+ PyObject *value = NULL;
+ if (meth_found) {
+ value = PyObject_Vectorcall(get, args, 3, NULL);
+ }
+ else {
+ value = PyObject_Vectorcall(get, &args[1], 2, NULL);
+ }
if (value == NULL) {
goto fail;
} |
@Fidget-Spinner
|
With new commit
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
@Fidget-Spinner Thanks for the review. Here is the final benchmark with optimization build with thin LTO :)
|
https://bugs.python.org/issue45045