Skip to content

Commit 75bb07e

Browse files
sebergmethane
authored andcommitted
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.
1 parent 50d4f12 commit 75bb07e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Slightly improve the speed of keyword argument parsing with many kwargs by strengthening the assumption that kwargs are interned strings.

Python/getargs.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,14 +2053,18 @@ find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
20532053
Py_ssize_t i, nkwargs;
20542054

20552055
nkwargs = PyTuple_GET_SIZE(kwnames);
2056-
for (i=0; i < nkwargs; i++) {
2056+
for (i = 0; i < nkwargs; i++) {
20572057
PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
20582058

2059-
/* ptr==ptr should match in most cases since keyword keys
2060-
should be interned strings */
2059+
/* kwname == key will normally find a match in since keyword keys
2060+
should be interned strings; if not retry below in a new loop. */
20612061
if (kwname == key) {
20622062
return kwstack[i];
20632063
}
2064+
}
2065+
2066+
for (i = 0; i < nkwargs; i++) {
2067+
PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
20642068
assert(PyUnicode_Check(kwname));
20652069
if (_PyUnicode_EQ(kwname, key)) {
20662070
return kwstack[i];

0 commit comments

Comments
 (0)