diff --git a/Include/abstract.h b/Include/abstract.h index 8969019e30c018..dd25b696ed8543 100644 --- a/Include/abstract.h +++ b/Include/abstract.h @@ -180,6 +180,15 @@ PyAPI_FUNC(PyObject *) _PyStack_AsDict( PyObject **values, PyObject *kwnames); +/* Combine _PyStack_AsTuple() with _PyStack_AsDict(). + Return 0 on success, or -1 on error. */ +PyAPI_FUNC(int) _PyStack_AsTupleAndDict( + PyObject **args, + Py_ssize_t nargs, + PyObject *kwnames, + PyObject **tuple, + PyObject **dict); + /* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple). Return 0 on success, raise an exception and return -1 on error. diff --git a/Include/descrobject.h b/Include/descrobject.h index cb43174838a86b..97023a5d905b65 100644 --- a/Include/descrobject.h +++ b/Include/descrobject.h @@ -23,6 +23,9 @@ typedef PyObject *(*wrapperfunc)(PyObject *self, PyObject *args, typedef PyObject *(*wrapperfunc_kwds)(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds); +typedef PyObject *(*wrapperfastfunc)(PyObject *self, PyObject **args, + Py_ssize_t nargs, void *wrapped); + struct wrapperbase { const char *name; int offset; @@ -31,6 +34,7 @@ struct wrapperbase { const char *doc; int flags; PyObject *name_strobj; + wrapperfastfunc fastwrapper; }; /* Flags for above struct */ diff --git a/Objects/call.c b/Objects/call.c index 91e607831177c3..c12f08db7806e8 100644 --- a/Objects/call.c +++ b/Objects/call.c @@ -80,6 +80,11 @@ _Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where) /* --- Core PyObject call functions ------------------------------- */ +extern PyObject * +_PyWrapperDesr_FastCallDict(PyObject *descr, + PyObject **args, Py_ssize_t argc, + PyObject *kwargs); + PyObject * _PyObject_FastCallDict(PyObject *callable, PyObject **args, Py_ssize_t nargs, PyObject *kwargs) @@ -100,6 +105,9 @@ _PyObject_FastCallDict(PyObject *callable, PyObject **args, Py_ssize_t nargs, else if (PyCFunction_Check(callable)) { return _PyCFunction_FastCallDict(callable, args, nargs, kwargs); } + else if (Py_TYPE(callable) == &PyWrapperDescr_Type) { + return _PyWrapperDesr_FastCallDict(callable, args, nargs, kwargs); + } else { PyObject *argstuple, *result; ternaryfunc call; @@ -132,6 +140,10 @@ _PyObject_FastCallDict(PyObject *callable, PyObject **args, Py_ssize_t nargs, } } +extern PyObject * +_PyWrapperDesr_FastCallKeywords(PyObject *descr, + PyObject **args, Py_ssize_t argc, + PyObject *kwnames); PyObject * _PyObject_FastCallKeywords(PyObject *callable, PyObject **stack, Py_ssize_t nargs, @@ -155,6 +167,9 @@ _PyObject_FastCallKeywords(PyObject *callable, PyObject **stack, Py_ssize_t narg if (PyCFunction_Check(callable)) { return _PyCFunction_FastCallKeywords(callable, stack, nargs, kwnames); } + else if (Py_TYPE(callable) == &PyWrapperDescr_Type) { + return _PyWrapperDesr_FastCallKeywords(callable, stack, nargs, kwnames); + } else { /* Slow-path: build a temporary tuple for positional arguments and a temporary dictionary for keyword arguments (if any) */ @@ -174,22 +189,11 @@ _PyObject_FastCallKeywords(PyObject *callable, PyObject **stack, Py_ssize_t narg return NULL; } - argstuple = _PyStack_AsTuple(stack, nargs); - if (argstuple == NULL) { + if (_PyStack_AsTupleAndDict(stack, nargs, kwnames, + &argstuple, &kwdict) < 0) { return NULL; } - if (nkwargs > 0) { - kwdict = _PyStack_AsDict(stack + nargs, kwnames); - if (kwdict == NULL) { - Py_DECREF(argstuple); - return NULL; - } - } - else { - kwdict = NULL; - } - if (Py_EnterRecursiveCall(" while calling a Python object")) { Py_DECREF(argstuple); Py_XDECREF(kwdict); @@ -663,34 +667,32 @@ _PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject * { /* Slow-path: create a temporary tuple for positional arguments and a temporary dict for keyword arguments */ - PyObject *argtuple; - - argtuple = _PyStack_AsTuple(args, nargs); - if (argtuple == NULL) { - goto exit; - } if (flags & METH_KEYWORDS) { - PyObject *kwdict; - - if (nkwargs > 0) { - kwdict = _PyStack_AsDict(args + nargs, kwnames); - if (kwdict == NULL) { - Py_DECREF(argtuple); - goto exit; - } - } - else { - kwdict = NULL; + PyObject *argtuple, *kwdict; + + if (_PyStack_AsTupleAndDict(args, nargs, kwnames, + &argtuple, &kwdict) < 0) { + return NULL; } result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict); + + Py_DECREF(argtuple); Py_XDECREF(kwdict); } else { + PyObject *argtuple; + + argtuple = _PyStack_AsTuple(args, nargs); + if (argtuple == NULL) { + goto exit; + } + result = (*meth) (self, argtuple); + + Py_DECREF(argtuple); } - Py_DECREF(argtuple); break; } @@ -1339,6 +1341,30 @@ _PyStack_AsDict(PyObject **values, PyObject *kwnames) } +int +_PyStack_AsTupleAndDict(PyObject **args, Py_ssize_t nargs, PyObject *kwnames, + PyObject **tuple, PyObject **dict) +{ + *tuple = _PyStack_AsTuple(args, nargs); + if (*tuple == NULL) { + return -1; + } + + Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames); + if (nkwargs > 0) { + *dict = _PyStack_AsDict(args + nargs, kwnames); + if (*dict == NULL) { + Py_DECREF(*tuple); + return -1; + } + } + else { + *dict = NULL; + } + return 0; +} + + int _PyStack_UnpackDict(PyObject **args, Py_ssize_t nargs, PyObject *kwargs, PyObject ***p_stack, PyObject **p_kwnames) diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 5dc27ef67278a4..82c5621115c77f 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -357,11 +357,68 @@ wrapperdescr_raw_call(PyWrapperDescrObject *descr, PyObject *self, return (*wrapper)(self, args, descr->d_wrapped); } +Py_LOCAL_INLINE(PyObject *) +wrapperdescr_raw_fastcall_dict(PyWrapperDescrObject *descr, PyObject *self, + PyObject **args, Py_ssize_t nargs, + PyObject *kwds) +{ + PyObject *args_tuple, *res; + + if (descr->d_base->fastwrapper) { + if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_GET_SIZE(kwds) != 0)) { + PyErr_Format(PyExc_TypeError, + "wrapper %s() takes no keyword arguments", + descr->d_base->name); + return NULL; + } + + return descr->d_base->fastwrapper(self, args, nargs, descr->d_wrapped); + } + + args_tuple = _PyStack_AsTuple(args, nargs); + if (args_tuple == NULL) { + return NULL; + } + res = wrapperdescr_raw_call(descr, self, args_tuple, kwds); + Py_DECREF(args_tuple); + return res; +} + +Py_LOCAL_INLINE(PyObject *) +wrapperdescr_raw_fastcall_keywords(PyWrapperDescrObject *descr, PyObject *self, + PyObject **args, Py_ssize_t nargs, + PyObject *kwnames) +{ + PyObject *argtuple, *kwdict, *res; + + if (descr->d_base->fastwrapper) { + assert(kwnames == NULL || PyTuple_Check(kwnames)); + if (kwnames != NULL && PyTuple_GET_SIZE(kwnames) != 0) { + PyErr_Format(PyExc_TypeError, + "wrapper %s() takes no keyword arguments", + descr->d_base->name); + return NULL; + } + + return descr->d_base->fastwrapper(self, args, nargs, descr->d_wrapped); + } + + if (_PyStack_AsTupleAndDict(args, nargs, kwnames, + &argtuple, &kwdict) < 0) { + return NULL; + } + res = wrapperdescr_raw_call(descr, self, argtuple, kwdict); + Py_DECREF(argtuple); + Py_XDECREF(kwdict); + + return res; +} + static PyObject * wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds) { Py_ssize_t argc; - PyObject *self, *result; + PyObject *self, **stack; /* Make sure that the first argument is acceptable as 'self' */ assert(PyTuple_Check(args)); @@ -387,13 +444,93 @@ wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds) return NULL; } - args = PyTuple_GetSlice(args, 1, argc); - if (args == NULL) { + if (argc > 1) { + stack = &PyTuple_GET_ITEM(args, 1); + } + else { + stack = NULL; + } + return wrapperdescr_raw_fastcall_dict(descr, self, + stack, + argc - 1, + kwds); +} + +PyObject * +_PyWrapperDesr_FastCallKeywords(PyObject *descro, + PyObject **args, Py_ssize_t argc, + PyObject *kwnames) +{ + PyObject *self; + + assert(Py_TYPE(descro) == &PyWrapperDescr_Type); + PyWrapperDescrObject *descr = (PyWrapperDescrObject *)descro; + + /* Make sure that the first argument is acceptable as 'self' */ + if (argc < 1) { + PyErr_Format(PyExc_TypeError, + "descriptor '%V' of '%.100s' " + "object needs an argument", + descr_name((PyDescrObject *)descr), "?", + PyDescr_TYPE(descr)->tp_name); + return NULL; + } + self = args[0]; + if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self), + (PyObject *)PyDescr_TYPE(descr))) { + PyErr_Format(PyExc_TypeError, + "descriptor '%V' " + "requires a '%.100s' object " + "but received a '%.100s'", + descr_name((PyDescrObject *)descr), "?", + PyDescr_TYPE(descr)->tp_name, + self->ob_type->tp_name); + return NULL; + } + + return wrapperdescr_raw_fastcall_keywords(descr, self, + args + 1, + argc - 1, + kwnames); +} + + +PyObject * +_PyWrapperDesr_FastCallDict(PyObject *descro, + PyObject **args, Py_ssize_t argc, + PyObject *kwargs) +{ + PyObject *self; + + assert(Py_TYPE(descro) == &PyWrapperDescr_Type); + PyWrapperDescrObject *descr = (PyWrapperDescrObject *)descro; + + /* Make sure that the first argument is acceptable as 'self' */ + if (argc < 1) { + PyErr_Format(PyExc_TypeError, + "descriptor '%V' of '%.100s' " + "object needs an argument", + descr_name((PyDescrObject *)descr), "?", + PyDescr_TYPE(descr)->tp_name); return NULL; } - result = wrapperdescr_raw_call(descr, self, args, kwds); - Py_DECREF(args); - return result; + self = args[0]; + if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self), + (PyObject *)PyDescr_TYPE(descr))) { + PyErr_Format(PyExc_TypeError, + "descriptor '%V' " + "requires a '%.100s' object " + "but received a '%.100s'", + descr_name((PyDescrObject *)descr), "?", + PyDescr_TYPE(descr)->tp_name, + self->ob_type->tp_name); + return NULL; + } + + return wrapperdescr_raw_fastcall_dict(descr, self, + args + 1, + argc - 1, + kwargs); } diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 5e0d81f6859136..1ffe71511d7b39 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5307,6 +5307,19 @@ check_num_args(PyObject *ob, int n) return 0; } +static int +check_fast_num_args(Py_ssize_t nargs, Py_ssize_t n) +{ + if (n == nargs) { + return 1; + } + + PyErr_Format( + PyExc_TypeError, + "expected %d arguments, got %zd", n, nargs); + return 0; +} + /* Generic wrappers for overloadable 'operators' such as __getitem__ */ /* There's a wrapper *function* for each distinct function typedef used @@ -5329,6 +5342,23 @@ wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped) return PyLong_FromLong((long)res); } +static PyObject * +fastwrap_lenfunc(PyObject *self, PyObject **args, Py_ssize_t nargs, + void *wrapped) +{ + lenfunc func = (lenfunc)wrapped; + Py_ssize_t res; + + if (!check_fast_num_args(nargs, 0)) { + return NULL; + } + res = (*func)(self); + if (res == -1 && PyErr_Occurred()) { + return NULL; + } + return PyLong_FromLong((long)res); +} + static PyObject * wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped) { @@ -5355,6 +5385,18 @@ wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) return (*func)(self, other); } +static PyObject * +fastwrap_binaryfunc(PyObject *self, PyObject **args, Py_ssize_t nargs, + void *wrapped) +{ + binaryfunc func = (binaryfunc)wrapped; + + if (!check_fast_num_args(nargs, 1)) { + return NULL; + } + return (*func)(self, args[0]); +} + static PyObject * wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) { @@ -5367,6 +5409,18 @@ wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) return (*func)(self, other); } +static PyObject * +fastwrap_binaryfunc_l(PyObject *self, PyObject **args, Py_ssize_t nargs, + void *wrapped) +{ + binaryfunc func = (binaryfunc)wrapped; + + if (!check_fast_num_args(nargs, 1)) { + return NULL; + } + return (*func)(self, args[0]); +} + static PyObject * wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) { @@ -5379,6 +5433,18 @@ wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) return (*func)(other, self); } +static PyObject * +fastwrap_binaryfunc_r(PyObject *self, PyObject **args, Py_ssize_t nargs, + void *wrapped) +{ + binaryfunc func = (binaryfunc)wrapped; + + if (!check_fast_num_args(nargs, 1)) { + return NULL; + } + return (*func)(args[0], self); +} + static PyObject * wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) { @@ -5417,6 +5483,18 @@ wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) return (*func)(self); } +static PyObject * +fastwrap_unaryfunc(PyObject *self, PyObject **args, Py_ssize_t nargs, + void *wrapped) +{ + unaryfunc func = (unaryfunc)wrapped; + + if (!check_fast_num_args(nargs, 0)) { + return NULL; + } + return (*func)(self); +} + static PyObject * wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped) { @@ -5650,6 +5728,18 @@ wrap_del(PyObject *self, PyObject *args, void *wrapped) Py_RETURN_NONE; } +static PyObject * +fastwrap_del(PyObject *self, PyObject **args, Py_ssize_t nargs, void *wrapped) +{ + destructor func = (destructor)wrapped; + + if (!check_fast_num_args(nargs, 0)) + return NULL; + + (*func)(self); + Py_RETURN_NONE; +} + static PyObject * wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) { @@ -7066,6 +7156,25 @@ init_slotdefs(void) p->name_strobj = PyUnicode_InternFromString(p->name); if (!p->name_strobj || !PyUnicode_CHECK_INTERNED(p->name_strobj)) Py_FatalError("Out of memory interning slotdef names"); + /* FIXME: update directly slotdefs. It's just that I was too lazy :-) */ + if (p->wrapper == wrap_unaryfunc) { + p->fastwrapper = fastwrap_unaryfunc; + } + else if (p->wrapper == wrap_binaryfunc) { + p->fastwrapper = fastwrap_binaryfunc; + } + else if (p->wrapper == wrap_binaryfunc_l) { + p->fastwrapper = fastwrap_binaryfunc_l; + } + else if (p->wrapper == wrap_binaryfunc_r) { + p->fastwrapper = fastwrap_binaryfunc_r; + } + else if (p->wrapper == wrap_lenfunc) { + p->fastwrapper = fastwrap_lenfunc; + } + else if (p->wrapper == wrap_del) { + p->fastwrapper = fastwrap_del; + } } slotdefs_initialized = 1; }