Issue #29263: LOAD_METHOD support for C methods
Calling builtin method is at most 10% faster.
This commit is contained in:
parent
144fff8b90
commit
5566bbb8d5
|
@ -90,6 +90,9 @@ PyAPI_FUNC(PyObject *) PyDescr_NewMember(PyTypeObject *,
|
||||||
PyAPI_FUNC(PyObject *) PyDescr_NewGetSet(PyTypeObject *,
|
PyAPI_FUNC(PyObject *) PyDescr_NewGetSet(PyTypeObject *,
|
||||||
struct PyGetSetDef *);
|
struct PyGetSetDef *);
|
||||||
#ifndef Py_LIMITED_API
|
#ifndef Py_LIMITED_API
|
||||||
|
|
||||||
|
PyAPI_FUNC(PyObject *) _PyMethodDescr_FastCallKeywords(
|
||||||
|
PyObject *descrobj, PyObject **stack, Py_ssize_t nargs, PyObject *kwnames);
|
||||||
PyAPI_FUNC(PyObject *) PyDescr_NewWrapper(PyTypeObject *,
|
PyAPI_FUNC(PyObject *) PyDescr_NewWrapper(PyTypeObject *,
|
||||||
struct wrapperbase *, void *);
|
struct wrapperbase *, void *);
|
||||||
#define PyDescr_IsData(d) (Py_TYPE(d)->tp_descr_set != NULL)
|
#define PyDescr_IsData(d) (Py_TYPE(d)->tp_descr_set != NULL)
|
||||||
|
|
|
@ -102,6 +102,13 @@ PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallDict(
|
||||||
PyObject **args,
|
PyObject **args,
|
||||||
Py_ssize_t nargs,
|
Py_ssize_t nargs,
|
||||||
PyObject *kwargs);
|
PyObject *kwargs);
|
||||||
|
|
||||||
|
PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallKeywords(
|
||||||
|
PyMethodDef *method,
|
||||||
|
PyObject *self,
|
||||||
|
PyObject **args,
|
||||||
|
Py_ssize_t nargs,
|
||||||
|
PyObject *kwnames);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
|
PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);
|
||||||
|
|
|
@ -846,7 +846,7 @@ id(42)
|
||||||
breakpoint='time_gmtime',
|
breakpoint='time_gmtime',
|
||||||
cmds_after_breakpoint=['py-bt-full'],
|
cmds_after_breakpoint=['py-bt-full'],
|
||||||
)
|
)
|
||||||
self.assertIn('#1 <built-in method gmtime', gdb_output)
|
self.assertIn('#2 <built-in method gmtime', gdb_output)
|
||||||
|
|
||||||
@unittest.skipIf(python_is_optimized(),
|
@unittest.skipIf(python_is_optimized(),
|
||||||
"Python was compiled with optimizations")
|
"Python was compiled with optimizations")
|
||||||
|
|
|
@ -246,6 +246,44 @@ methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwargs)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// same to methoddescr_call(), but use FASTCALL convention.
|
||||||
|
PyObject *
|
||||||
|
_PyMethodDescr_FastCallKeywords(PyObject *descrobj,
|
||||||
|
PyObject **args, Py_ssize_t nargs,
|
||||||
|
PyObject *kwnames)
|
||||||
|
{
|
||||||
|
assert(Py_TYPE(descrobj) == &PyMethodDescr_Type);
|
||||||
|
PyMethodDescrObject *descr = (PyMethodDescrObject *)descrobj;
|
||||||
|
PyObject *self, *result;
|
||||||
|
|
||||||
|
/* Make sure that the first argument is acceptable as 'self' */
|
||||||
|
if (nargs < 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = _PyMethodDef_RawFastCallKeywords(descr->d_method, self,
|
||||||
|
args+1, nargs-1, kwnames);
|
||||||
|
result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
|
classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
|
||||||
PyObject *kwds)
|
PyObject *kwds)
|
||||||
|
|
|
@ -215,32 +215,24 @@ _PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
|
_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
|
||||||
Py_ssize_t nargs, PyObject *kwnames)
|
Py_ssize_t nargs, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
PyCFunctionObject *func;
|
/* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
|
||||||
PyCFunction meth;
|
because it can clear it (directly or indirectly) and so the
|
||||||
PyObject *self, *result;
|
caller loses its exception */
|
||||||
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
|
assert(!PyErr_Occurred());
|
||||||
int flags;
|
|
||||||
|
|
||||||
assert(func_obj != NULL);
|
assert(method != NULL);
|
||||||
assert(PyCFunction_Check(func_obj));
|
|
||||||
assert(nargs >= 0);
|
assert(nargs >= 0);
|
||||||
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
|
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
|
||||||
assert((nargs == 0 && nkwargs == 0) || args != NULL);
|
|
||||||
/* kwnames must only contains str strings, no subclass, and all keys must
|
/* kwnames must only contains str strings, no subclass, and all keys must
|
||||||
be unique */
|
be unique */
|
||||||
|
|
||||||
/* _PyCFunction_FastCallKeywords() must not be called with an exception
|
PyCFunction meth = method->ml_meth;
|
||||||
set, because it can clear it (directly or indirectly) and so the caller
|
int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
|
||||||
loses its exception */
|
Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames);
|
||||||
assert(!PyErr_Occurred());
|
PyObject *result;
|
||||||
|
|
||||||
func = (PyCFunctionObject*)func_obj;
|
|
||||||
meth = PyCFunction_GET_FUNCTION(func);
|
|
||||||
self = PyCFunction_GET_SELF(func);
|
|
||||||
flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
|
|
||||||
|
|
||||||
switch (flags)
|
switch (flags)
|
||||||
{
|
{
|
||||||
|
@ -248,7 +240,7 @@ _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
|
||||||
if (nargs != 0) {
|
if (nargs != 0) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,7 +255,7 @@ _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -326,16 +318,31 @@ _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
|
||||||
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 keyword arguments",
|
"%.200s() takes no keyword arguments",
|
||||||
func->m_ml->ml_name);
|
method->ml_name);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyObject *
|
||||||
|
_PyCFunction_FastCallKeywords(PyObject *func, PyObject **args,
|
||||||
|
Py_ssize_t nargs, PyObject *kwnames)
|
||||||
|
{
|
||||||
|
PyObject *result;
|
||||||
|
|
||||||
|
assert(func != NULL);
|
||||||
|
assert(PyCFunction_Check(func));
|
||||||
|
|
||||||
|
result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
|
||||||
|
PyCFunction_GET_SELF(func),
|
||||||
|
args, nargs, kwnames);
|
||||||
|
result = _Py_CheckFunctionResult(func, result, NULL);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/* Methods (the standard built-in methods, that is) */
|
/* Methods (the standard built-in methods, that is) */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
|
@ -1060,8 +1060,8 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
|
||||||
descr = _PyType_Lookup(tp, name);
|
descr = _PyType_Lookup(tp, name);
|
||||||
if (descr != NULL) {
|
if (descr != NULL) {
|
||||||
Py_INCREF(descr);
|
Py_INCREF(descr);
|
||||||
if (PyFunction_Check(descr)) {
|
if (PyFunction_Check(descr) ||
|
||||||
/* A python method. */
|
(Py_TYPE(descr) == &PyMethodDescr_Type)) {
|
||||||
meth_found = 1;
|
meth_found = 1;
|
||||||
} else {
|
} else {
|
||||||
f = descr->ob_type->tp_descr_get;
|
f = descr->ob_type->tp_descr_get;
|
||||||
|
|
|
@ -4832,17 +4832,19 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
|
||||||
PyObject *x, *w;
|
PyObject *x, *w;
|
||||||
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
|
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
|
||||||
Py_ssize_t nargs = oparg - nkwargs;
|
Py_ssize_t nargs = oparg - nkwargs;
|
||||||
PyObject **stack;
|
PyObject **stack = (*pp_stack) - nargs - nkwargs;
|
||||||
|
|
||||||
/* Always dispatch PyCFunction first, because these are
|
/* Always dispatch PyCFunction first, because these are
|
||||||
presumed to be the most frequent callable object.
|
presumed to be the most frequent callable object.
|
||||||
*/
|
*/
|
||||||
if (PyCFunction_Check(func)) {
|
if (PyCFunction_Check(func)) {
|
||||||
PyThreadState *tstate = PyThreadState_GET();
|
PyThreadState *tstate = PyThreadState_GET();
|
||||||
|
|
||||||
stack = (*pp_stack) - nargs - nkwargs;
|
|
||||||
C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
|
C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
|
||||||
}
|
}
|
||||||
|
else if (Py_TYPE(func) == &PyMethodDescr_Type) {
|
||||||
|
PyThreadState *tstate = PyThreadState_GET();
|
||||||
|
C_TRACE(x, _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames));
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
|
if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
|
||||||
/* Optimize access to bound methods. Reuse the Python stack
|
/* Optimize access to bound methods. Reuse the Python stack
|
||||||
|
@ -4856,20 +4858,18 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
|
||||||
Py_INCREF(func);
|
Py_INCREF(func);
|
||||||
Py_SETREF(*pfunc, self);
|
Py_SETREF(*pfunc, self);
|
||||||
nargs++;
|
nargs++;
|
||||||
|
stack--;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Py_INCREF(func);
|
Py_INCREF(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
stack = (*pp_stack) - nargs - nkwargs;
|
|
||||||
|
|
||||||
if (PyFunction_Check(func)) {
|
if (PyFunction_Check(func)) {
|
||||||
x = fast_function(func, stack, nargs, kwnames);
|
x = fast_function(func, stack, nargs, kwnames);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
|
x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_DECREF(func);
|
Py_DECREF(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1540,10 +1540,7 @@ class Frame(object):
|
||||||
|
|
||||||
if caller in ('_PyCFunction_FastCallDict',
|
if caller in ('_PyCFunction_FastCallDict',
|
||||||
'_PyCFunction_FastCallKeywords'):
|
'_PyCFunction_FastCallKeywords'):
|
||||||
if caller == '_PyCFunction_FastCallKeywords':
|
arg_name = 'func'
|
||||||
arg_name = 'func_obj'
|
|
||||||
else:
|
|
||||||
arg_name = 'func'
|
|
||||||
# Within that frame:
|
# Within that frame:
|
||||||
# "func" is the local containing the PyObject* of the
|
# "func" is the local containing the PyObject* of the
|
||||||
# PyCFunctionObject instance
|
# PyCFunctionObject instance
|
||||||
|
|
Loading…
Reference in New Issue