Skip to content

bpo-44160: speed up searching for keywords by using a dictionary #26200

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/cpython/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct PyCodeObject {
for tracebacks and debuggers; otherwise, constant de-duplication
would collapse identical functions/lambdas defined on different lines.
*/
PyObject *co_kwarg2index; /* Maps keyword arg names to index in locals */
Py_ssize_t *co_cell2arg; /* Maps cell vars which are arguments. */
PyObject *co_filename; /* unicode (where it was loaded from) */
PyObject *co_name; /* unicode (name, for reference) */
Expand Down Expand Up @@ -172,6 +173,8 @@ PyAPI_FUNC(int) _PyCode_GetExtra(PyObject *code, Py_ssize_t index,
PyAPI_FUNC(int) _PyCode_SetExtra(PyObject *code, Py_ssize_t index,
void *extra);

int _PyCode_InitKwarg2Index(PyCodeObject *co);

/** API for initializing the line number table. */
int _PyCode_InitAddressRange(PyCodeObject* co, PyCodeAddressRange *bounds);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Calling with keyword arguments is now linear, rather than quadratic, in the number of keyword arguments.
33 changes: 33 additions & 0 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
co->co_freevars = freevars;
Py_INCREF(cellvars);
co->co_cellvars = cellvars;
co->co_kwarg2index = NULL; // Lazily initialized
co->co_cell2arg = cell2arg;
Py_INCREF(filename);
co->co_filename = filename;
Expand All @@ -274,6 +275,37 @@ PyCode_NewWithPosOnlyArgs(int argcount, int posonlyargcount, int kwonlyargcount,
return co;
}

int
_PyCode_InitKwarg2Index(PyCodeObject *co)
{
assert(co->co_kwarg2index == NULL);
Py_ssize_t total_args =
(Py_ssize_t)co->co_argcount + (Py_ssize_t)co->co_kwonlyargcount;
PyObject *d = PyDict_New();
if (d == NULL) {
return -1;
}

for (Py_ssize_t j = co->co_posonlyargcount; j < total_args; j++) {
PyObject *index = PyLong_FromSsize_t(j);
if (index == NULL) {
Py_DECREF(d);
return -1;
}
PyObject *kw = PyTuple_GET_ITEM(co->co_varnames, j);
Py_INCREF(kw);
if (PyDict_SetItem(d, kw, index) < 0) {
Py_DECREF(index);
Py_DECREF(kw);
Py_DECREF(d);
return -1;
}
}

co->co_kwarg2index = d;
return 0;
}

PyCodeObject *
PyCode_New(int argcount, int kwonlyargcount,
int nlocals, int stacksize, int flags,
Expand Down Expand Up @@ -672,6 +704,7 @@ code_dealloc(PyCodeObject *co)
Py_XDECREF(co->co_name);
Py_XDECREF(co->co_linetable);
Py_XDECREF(co->co_exceptiontable);
Py_XDECREF(co->co_kwarg2index);
if (co->co_cell2arg != NULL)
PyMem_Free(co->co_cell2arg);
if (co->co_zombieframe != NULL)
Expand Down
49 changes: 16 additions & 33 deletions Python/ceval.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/* Execute compiled code */

/* XXX TO DO:
XXX speed up searching for keywords by using a dictionary
XXX document it!
*/

/* enable more aggressive intra-module optimizations, where available */
/* affects both release and debug builds - see bpo-43271 */
#define PY_LOCAL_AGGRESSIVE
Expand Down Expand Up @@ -262,9 +257,6 @@ UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
}


#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "ceval_gil.h"

void _Py_NO_RETURN
Expand Down Expand Up @@ -4941,8 +4933,12 @@ _PyEval_MakeFrameVector(PyThreadState *tstate,
/* Handle keyword arguments */
if (kwnames != NULL) {
Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
if (kwcount > 0 && co->co_kwarg2index == NULL) {
if (_PyCode_InitKwarg2Index(co) < 0) {
goto fail;
}
}
for (i = 0; i < kwcount; i++) {
PyObject **co_varnames;
PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
PyObject *value = args[i+argcount];
Py_ssize_t j;
Expand All @@ -4954,42 +4950,29 @@ _PyEval_MakeFrameVector(PyThreadState *tstate,
goto fail;
}

/* Speed hack: do raw pointer compares. As names are
normally interned this should almost always hit. */
co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
for (j = co->co_posonlyargcount; j < total_args; j++) {
PyObject *varname = co_varnames[j];
if (varname == keyword) {
goto kw_found;
}
}

/* Slow fallback, just in case */
for (j = co->co_posonlyargcount; j < total_args; j++) {
PyObject *varname = co_varnames[j];
int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
if (cmp > 0) {
goto kw_found;
}
else if (cmp < 0) {
PyObject *index = PyDict_GetItemWithError(co->co_kwarg2index, keyword);
if (index != NULL) {
j = PyLong_AsSsize_t(index);
if (j == -1 && PyErr_Occurred()) {
goto fail;
}
goto kw_found;
}
else if (PyErr_Occurred()) {
goto fail;
}

assert(j >= total_args);
if (kwdict == NULL) {

if (co->co_posonlyargcount
&& positional_only_passed_as_keyword(tstate, co,
kwcount, kwnames,
con->fc_qualname))
con->fc_qualname))
{
goto fail;
}

_PyErr_Format(tstate, PyExc_TypeError,
"%U() got an unexpected keyword argument '%S'",
con->fc_qualname, keyword);
"%U() got an unexpected keyword argument '%S'",
con->fc_qualname, keyword);
goto fail;
}

Expand Down