Optimize methoddescr_call(): avoid temporary PyCFunction

Issue #29259, #29263. methoddescr_call() creates a PyCFunction object, call it
and the destroy it. Add a new _PyMethodDef_RawFastCallDict() method to avoid
the temporary PyCFunction object.
This commit is contained in:
Victor Stinner 2017-01-18 10:38:09 +01:00
parent 35ecebe165
commit c52572319c
3 changed files with 40 additions and 26 deletions

View File

@ -95,6 +95,13 @@ typedef struct {
PyObject *m_module; /* The __module__ attribute, can be anything */ PyObject *m_module; /* The __module__ attribute, can be anything */
PyObject *m_weakreflist; /* List of weak references */ PyObject *m_weakreflist; /* List of weak references */
} PyCFunctionObject; } PyCFunctionObject;
PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallDict(
PyMethodDef *method,
PyObject *self,
PyObject **args,
Py_ssize_t nargs,
PyObject *kwargs);
#endif #endif
PyAPI_FUNC(int) PyCFunction_ClearFreeList(void); PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);

View File

@ -210,15 +210,15 @@ getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
} }
static PyObject * static PyObject *
methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds) methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwargs)
{ {
Py_ssize_t argc; Py_ssize_t nargs;
PyObject *self, *func, *result, **stack; PyObject *self, *result;
/* Make sure that the first argument is acceptable as 'self' */ /* Make sure that the first argument is acceptable as 'self' */
assert(PyTuple_Check(args)); assert(PyTuple_Check(args));
argc = PyTuple_GET_SIZE(args); nargs = PyTuple_GET_SIZE(args);
if (argc < 1) { if (nargs < 1) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"descriptor '%V' of '%.100s' " "descriptor '%V' of '%.100s' "
"object needs an argument", "object needs an argument",
@ -239,12 +239,10 @@ methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
func = PyCFunction_NewEx(descr->d_method, self, NULL); result = _PyMethodDef_RawFastCallDict(descr->d_method, self,
if (func == NULL) &PyTuple_GET_ITEM(args, 1), nargs - 1,
return NULL; kwargs);
stack = &PyTuple_GET_ITEM(args, 1); result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
result = _PyObject_FastCallDict(func, stack, argc - 1, kwds);
Py_DECREF(func);
return result; return result;
} }

View File

@ -152,17 +152,14 @@ PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwds)
} }
PyObject * PyObject *
_PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs, _PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, PyObject **args,
PyObject *kwargs) Py_ssize_t nargs, PyObject *kwargs)
{ {
PyCFunctionObject *func;
PyCFunction meth; PyCFunction meth;
PyObject *self;
PyObject *result; PyObject *result;
int flags; int flags;
assert(func_obj != NULL); assert(method != NULL);
assert(PyCFunction_Check(func_obj));
assert(nargs >= 0); assert(nargs >= 0);
assert(nargs == 0 || args != NULL); assert(nargs == 0 || args != NULL);
assert(kwargs == NULL || PyDict_Check(kwargs)); assert(kwargs == NULL || PyDict_Check(kwargs));
@ -172,10 +169,8 @@ _PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
caller loses its exception */ caller loses its exception */
assert(!PyErr_Occurred()); assert(!PyErr_Occurred());
func = (PyCFunctionObject*)func_obj; meth = method->ml_meth;
meth = PyCFunction_GET_FUNCTION(func); flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
self = PyCFunction_GET_SELF(func);
flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
switch (flags) switch (flags)
{ {
@ -186,7 +181,7 @@ _PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) { if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
func->m_ml->ml_name); method->ml_name);
return NULL; return NULL;
} }
@ -197,7 +192,7 @@ _PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
if (nargs != 1) { if (nargs != 1) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s() takes exactly one argument (%zd given)", "%.200s() takes exactly one argument (%zd given)",
func->m_ml->ml_name, nargs); method->ml_name, nargs);
return NULL; return NULL;
} }
@ -259,17 +254,31 @@ _PyCFunction_FastCallDict(PyObject *func_obj, PyObject **args, Py_ssize_t nargs,
return NULL; return NULL;
} }
result = _Py_CheckFunctionResult(func_obj, result, NULL);
return result; return result;
no_keyword_error: no_keyword_error:
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s() takes no arguments (%zd given)", "%.200s() takes no arguments (%zd given)",
func->m_ml->ml_name, nargs); method->ml_name, nargs);
return NULL; return NULL;
} }
PyObject *
_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
PyObject *kwargs)
{
PyObject *result;
assert(func != NULL);
assert(PyCFunction_Check(func));
result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
PyCFunction_GET_SELF(func),
args, nargs, kwargs);
result = _Py_CheckFunctionResult(func, result, NULL);
return result;
}
PyObject * PyObject *
_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args, _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
Py_ssize_t nargs, PyObject *kwnames) Py_ssize_t nargs, PyObject *kwnames)