Skip to content

Commit 5566bbb

Browse files
committed
Issue #29263: LOAD_METHOD support for C methods
Calling builtin method is at most 10% faster.
1 parent 144fff8 commit 5566bbb

File tree

8 files changed

+88
-36
lines changed

8 files changed

+88
-36
lines changed

Include/descrobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ PyAPI_FUNC(PyObject *) PyDescr_NewMember(PyTypeObject *,
9090
PyAPI_FUNC(PyObject *) PyDescr_NewGetSet(PyTypeObject *,
9191
struct PyGetSetDef *);
9292
#ifndef Py_LIMITED_API
93+
94+
PyAPI_FUNC(PyObject *) _PyMethodDescr_FastCallKeywords(
95+
PyObject *descrobj, PyObject **stack, Py_ssize_t nargs, PyObject *kwnames);
9396
PyAPI_FUNC(PyObject *) PyDescr_NewWrapper(PyTypeObject *,
9497
struct wrapperbase *, void *);
9598
#define PyDescr_IsData(d) (Py_TYPE(d)->tp_descr_set != NULL)

Include/methodobject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallDict(
102102
PyObject **args,
103103
Py_ssize_t nargs,
104104
PyObject *kwargs);
105+
106+
PyAPI_FUNC(PyObject *) _PyMethodDef_RawFastCallKeywords(
107+
PyMethodDef *method,
108+
PyObject *self,
109+
PyObject **args,
110+
Py_ssize_t nargs,
111+
PyObject *kwnames);
105112
#endif
106113

107114
PyAPI_FUNC(int) PyCFunction_ClearFreeList(void);

Lib/test/test_gdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ def test_pycfunction(self):
846846
breakpoint='time_gmtime',
847847
cmds_after_breakpoint=['py-bt-full'],
848848
)
849-
self.assertIn('#1 <built-in method gmtime', gdb_output)
849+
self.assertIn('#2 <built-in method gmtime', gdb_output)
850850

851851
@unittest.skipIf(python_is_optimized(),
852852
"Python was compiled with optimizations")

Objects/descrobject.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,44 @@ methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwargs)
246246
return result;
247247
}
248248

249+
// same to methoddescr_call(), but use FASTCALL convention.
250+
PyObject *
251+
_PyMethodDescr_FastCallKeywords(PyObject *descrobj,
252+
PyObject **args, Py_ssize_t nargs,
253+
PyObject *kwnames)
254+
{
255+
assert(Py_TYPE(descrobj) == &PyMethodDescr_Type);
256+
PyMethodDescrObject *descr = (PyMethodDescrObject *)descrobj;
257+
PyObject *self, *result;
258+
259+
/* Make sure that the first argument is acceptable as 'self' */
260+
if (nargs < 1) {
261+
PyErr_Format(PyExc_TypeError,
262+
"descriptor '%V' of '%.100s' "
263+
"object needs an argument",
264+
descr_name((PyDescrObject *)descr), "?",
265+
PyDescr_TYPE(descr)->tp_name);
266+
return NULL;
267+
}
268+
self = args[0];
269+
if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
270+
(PyObject *)PyDescr_TYPE(descr))) {
271+
PyErr_Format(PyExc_TypeError,
272+
"descriptor '%V' "
273+
"requires a '%.100s' object "
274+
"but received a '%.100s'",
275+
descr_name((PyDescrObject *)descr), "?",
276+
PyDescr_TYPE(descr)->tp_name,
277+
self->ob_type->tp_name);
278+
return NULL;
279+
}
280+
281+
result = _PyMethodDef_RawFastCallKeywords(descr->d_method, self,
282+
args+1, nargs-1, kwnames);
283+
result = _Py_CheckFunctionResult((PyObject *)descr, result, NULL);
284+
return result;
285+
}
286+
249287
static PyObject *
250288
classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
251289
PyObject *kwds)

Objects/methodobject.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,40 +215,32 @@ _PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
215215
}
216216

217217
PyObject *
218-
_PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
219-
Py_ssize_t nargs, PyObject *kwnames)
218+
_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
219+
Py_ssize_t nargs, PyObject *kwnames)
220220
{
221-
PyCFunctionObject *func;
222-
PyCFunction meth;
223-
PyObject *self, *result;
224-
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
225-
int flags;
221+
/* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
222+
because it can clear it (directly or indirectly) and so the
223+
caller loses its exception */
224+
assert(!PyErr_Occurred());
226225

227-
assert(func_obj != NULL);
228-
assert(PyCFunction_Check(func_obj));
226+
assert(method != NULL);
229227
assert(nargs >= 0);
230228
assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
231-
assert((nargs == 0 && nkwargs == 0) || args != NULL);
232229
/* kwnames must only contains str strings, no subclass, and all keys must
233230
be unique */
234231

235-
/* _PyCFunction_FastCallKeywords() must not be called with an exception
236-
set, because it can clear it (directly or indirectly) and so the caller
237-
loses its exception */
238-
assert(!PyErr_Occurred());
239-
240-
func = (PyCFunctionObject*)func_obj;
241-
meth = PyCFunction_GET_FUNCTION(func);
242-
self = PyCFunction_GET_SELF(func);
243-
flags = PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
232+
PyCFunction meth = method->ml_meth;
233+
int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
234+
Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames);
235+
PyObject *result;
244236

245237
switch (flags)
246238
{
247239
case METH_NOARGS:
248240
if (nargs != 0) {
249241
PyErr_Format(PyExc_TypeError,
250242
"%.200s() takes no arguments (%zd given)",
251-
func->m_ml->ml_name, nargs);
243+
method->ml_name, nargs);
252244
return NULL;
253245
}
254246

@@ -263,7 +255,7 @@ _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
263255
if (nargs != 1) {
264256
PyErr_Format(PyExc_TypeError,
265257
"%.200s() takes exactly one argument (%zd given)",
266-
func->m_ml->ml_name, nargs);
258+
method->ml_name, nargs);
267259
return NULL;
268260
}
269261

@@ -326,16 +318,31 @@ _PyCFunction_FastCallKeywords(PyObject *func_obj, PyObject **args,
326318
return NULL;
327319
}
328320

329-
result = _Py_CheckFunctionResult(func_obj, result, NULL);
330321
return result;
331322

332323
no_keyword_error:
333324
PyErr_Format(PyExc_TypeError,
334325
"%.200s() takes no keyword arguments",
335-
func->m_ml->ml_name);
326+
method->ml_name);
336327
return NULL;
337328
}
338329

330+
PyObject *
331+
_PyCFunction_FastCallKeywords(PyObject *func, PyObject **args,
332+
Py_ssize_t nargs, PyObject *kwnames)
333+
{
334+
PyObject *result;
335+
336+
assert(func != NULL);
337+
assert(PyCFunction_Check(func));
338+
339+
result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
340+
PyCFunction_GET_SELF(func),
341+
args, nargs, kwnames);
342+
result = _Py_CheckFunctionResult(func, result, NULL);
343+
return result;
344+
}
345+
339346
/* Methods (the standard built-in methods, that is) */
340347

341348
static void

Objects/object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,8 @@ _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
10601060
descr = _PyType_Lookup(tp, name);
10611061
if (descr != NULL) {
10621062
Py_INCREF(descr);
1063-
if (PyFunction_Check(descr)) {
1064-
/* A python method. */
1063+
if (PyFunction_Check(descr) ||
1064+
(Py_TYPE(descr) == &PyMethodDescr_Type)) {
10651065
meth_found = 1;
10661066
} else {
10671067
f = descr->ob_type->tp_descr_get;

Python/ceval.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4832,17 +4832,19 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
48324832
PyObject *x, *w;
48334833
Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
48344834
Py_ssize_t nargs = oparg - nkwargs;
4835-
PyObject **stack;
4835+
PyObject **stack = (*pp_stack) - nargs - nkwargs;
48364836

48374837
/* Always dispatch PyCFunction first, because these are
48384838
presumed to be the most frequent callable object.
48394839
*/
48404840
if (PyCFunction_Check(func)) {
48414841
PyThreadState *tstate = PyThreadState_GET();
4842-
4843-
stack = (*pp_stack) - nargs - nkwargs;
48444842
C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
48454843
}
4844+
else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4845+
PyThreadState *tstate = PyThreadState_GET();
4846+
C_TRACE(x, _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames));
4847+
}
48464848
else {
48474849
if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
48484850
/* Optimize access to bound methods. Reuse the Python stack
@@ -4856,20 +4858,18 @@ call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
48564858
Py_INCREF(func);
48574859
Py_SETREF(*pfunc, self);
48584860
nargs++;
4861+
stack--;
48594862
}
48604863
else {
48614864
Py_INCREF(func);
48624865
}
48634866

4864-
stack = (*pp_stack) - nargs - nkwargs;
4865-
48664867
if (PyFunction_Check(func)) {
48674868
x = fast_function(func, stack, nargs, kwnames);
48684869
}
48694870
else {
48704871
x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
48714872
}
4872-
48734873
Py_DECREF(func);
48744874
}
48754875

Tools/gdb/libpython.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,10 +1540,7 @@ def is_other_python_frame(self):
15401540

15411541
if caller in ('_PyCFunction_FastCallDict',
15421542
'_PyCFunction_FastCallKeywords'):
1543-
if caller == '_PyCFunction_FastCallKeywords':
1544-
arg_name = 'func_obj'
1545-
else:
1546-
arg_name = 'func'
1543+
arg_name = 'func'
15471544
# Within that frame:
15481545
# "func" is the local containing the PyObject* of the
15491546
# PyCFunctionObject instance

0 commit comments

Comments
 (0)