bpo-45045: Optimize mapping patterns of structural pattern matching#28043
Merged
corona10 merged 4 commits intopython:mainfrom Aug 30, 2021
Merged
bpo-45045: Optimize mapping patterns of structural pattern matching#28043corona10 merged 4 commits intopython:mainfrom
corona10 merged 4 commits intopython:mainfrom
Conversation
Member
Author
|
corona10
commented
Aug 29, 2021
| goto fail; | ||
| } | ||
| values = PyList_New(0); | ||
| values = PyTuple_New(nkeys); |
Member
Author
There was a problem hiding this comment.
The size of the tuple is predictable.
corona10
commented
Aug 29, 2021
Python/ceval.c
Outdated
| } | ||
| PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL); | ||
| PyObject *args[] = { key, dummy }; | ||
| PyObject *value = PyObject_Vectorcall(get, args, 2, NULL); |
Member
Author
There was a problem hiding this comment.
Just replacing PyObject_CallFunctionObjArgs shows a 2% performance enhancement on the micro benchmark.
Member
|
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;
} |
Member
Author
|
@Fidget-Spinner |
Member
Author
|
With new commit |
Member
Author
|
@Fidget-Spinner Thanks for the review. Here is the final benchmark with optimization build with thin LTO :) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://bugs.python.org/issue45045