mirror of
https://github.com/python/cpython.git
synced 2025-10-17 04:08:28 +00:00
bpo-39028: Performance enhancement in keyword extraction (GH-17576)
All keywords should first be checked for pointer identity. Only after that failed for all keywords (unlikely) should unicode equality be used. The original code would call unicode equality on any non-matching keyword argument. Meaning calling it often e.g. when a function has many kwargs but only the last one is provided.
This commit is contained in:
parent
50d4f12958
commit
75bb07e92b
2 changed files with 8 additions and 3 deletions
|
@ -2053,14 +2053,18 @@ find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
|
|||
Py_ssize_t i, nkwargs;
|
||||
|
||||
nkwargs = PyTuple_GET_SIZE(kwnames);
|
||||
for (i=0; i < nkwargs; i++) {
|
||||
for (i = 0; i < nkwargs; i++) {
|
||||
PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
|
||||
|
||||
/* ptr==ptr should match in most cases since keyword keys
|
||||
should be interned strings */
|
||||
/* kwname == key will normally find a match in since keyword keys
|
||||
should be interned strings; if not retry below in a new loop. */
|
||||
if (kwname == key) {
|
||||
return kwstack[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < nkwargs; i++) {
|
||||
PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
|
||||
assert(PyUnicode_Check(kwname));
|
||||
if (_PyUnicode_EQ(kwname, key)) {
|
||||
return kwstack[i];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue