Raymond's patch for #1819: speedup function calls with named parameters

(35% faster according to pybench)
This commit is contained in:
Antoine Pitrou 2008-07-25 22:13:52 +00:00
parent 0c37ae0464
commit c2cc80c64e
1 changed files with 30 additions and 22 deletions

View File

@ -2779,6 +2779,7 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
}
}
for (i = 0; i < kwcount; i++) {
PyObject **co_varnames;
PyObject *keyword = kws[2*i];
PyObject *value = kws[2*i + 1];
int j;
@ -2788,14 +2789,21 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
PyString_AsString(co->co_name));
goto fail;
}
/* XXX slow -- speed up using dictionary? */
/* Speed hack: do raw pointer compares. As names are
normally interned this should almost always hit. */
co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
for (j = 0; j < co->co_argcount; j++) {
PyObject *nm = PyTuple_GET_ITEM(
co->co_varnames, j);
PyObject *nm = co_varnames[j];
if (nm == keyword)
goto kw_found;
}
/* Slow fallback, just in case */
for (j = 0; j < co->co_argcount; j++) {
PyObject *nm = co_varnames[j];
int cmp = PyObject_RichCompareBool(
keyword, nm, Py_EQ);
if (cmp > 0)
break;
goto kw_found;
else if (cmp < 0)
goto fail;
}
@ -2812,20 +2820,20 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
goto fail;
}
PyDict_SetItem(kwdict, keyword, value);
continue;
}
else {
if (GETLOCAL(j) != NULL) {
PyErr_Format(PyExc_TypeError,
"%.200s() got multiple "
"values for keyword "
"argument '%.400s'",
PyString_AsString(co->co_name),
PyString_AsString(keyword));
goto fail;
}
Py_INCREF(value);
SETLOCAL(j, value);
kw_found:
if (GETLOCAL(j) != NULL) {
PyErr_Format(PyExc_TypeError,
"%.200s() got multiple "
"values for keyword "
"argument '%.400s'",
PyString_AsString(co->co_name),
PyString_AsString(keyword));
goto fail;
}
Py_INCREF(value);
SETLOCAL(j, value);
}
if (argcount < co->co_argcount) {
int m = co->co_argcount - defcount;