Issue #29263: LOAD_METHOD support for C methods
Calling builtin method is at most 10% faster.
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index 3fb34a3..20c0d36 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -246,6 +246,44 @@
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 *
classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
PyObject *kwds)
diff --git a/Objects/methodobject.c b/Objects/methodobject.c
index 35a827e..6618d78 100644
--- a/Objects/methodobject.c
+++ b/Objects/methodobject.c
@@ -215,32 +215,24 @@
}
PyObject *
-_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
- Py_ssize_t nargs, PyObject *kwnames)
+_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
+ Py_ssize_t nargs, PyObject *kwnames)
{
- PyCFunctionObject *func;
- PyCFunction meth;
- PyObject *self, *result;
- Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
- int flags;
+ /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
+ because it can clear it (directly or indirectly) and so the
+ caller loses its exception */
+ assert(!PyErr_Occurred());
- assert(func_obj != NULL);
- assert(PyCFunction_Check(func_obj));
+ assert(method != NULL);
assert(nargs >= 0);
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
be unique */
- /* _PyCFunction_FastCallKeywords() must not be called with an exception
- set, because it can clear it (directly or indirectly) and so the caller
- loses its exception */
- assert(!PyErr_Occurred());
-
- 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);
+ PyCFunction meth = method->ml_meth;
+ int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
+ Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames);
+ PyObject *result;
switch (flags)
{
@@ -248,7 +240,7 @@
if (nargs != 0) {
PyErr_Format(PyExc_TypeError,
"%.200s() takes no arguments (%zd given)",
- func->m_ml->ml_name, nargs);
+ method->ml_name, nargs);
return NULL;
}
@@ -263,7 +255,7 @@
if (nargs != 1) {
PyErr_Format(PyExc_TypeError,
"%.200s() takes exactly one argument (%zd given)",
- func->m_ml->ml_name, nargs);
+ method->ml_name, nargs);
return NULL;
}
@@ -326,16 +318,31 @@
return NULL;
}
- result = _Py_CheckFunctionResult(func_obj, result, NULL);
return result;
no_keyword_error:
PyErr_Format(PyExc_TypeError,
"%.200s() takes no keyword arguments",
- func->m_ml->ml_name);
+ method->ml_name);
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) */
static void
diff --git a/Objects/object.c b/Objects/object.c
index 93cdc10..5da6cff 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1060,8 +1060,8 @@
descr = _PyType_Lookup(tp, name);
if (descr != NULL) {
Py_INCREF(descr);
- if (PyFunction_Check(descr)) {
- /* A python method. */
+ if (PyFunction_Check(descr) ||
+ (Py_TYPE(descr) == &PyMethodDescr_Type)) {
meth_found = 1;
} else {
f = descr->ob_type->tp_descr_get;