FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44850: Improve the performance of methodcaller. by anntzer · Pull Request #27782 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Calls to ``operator.methodcaller`` are now 25-33% faster thanks to the use of
the vectorcall protocol.
59 changes: 57 additions & 2 deletions Modules/_operator.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Python.h"
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h"
#include "clinic/_operator.c.h"

typedef struct {
Expand Down Expand Up @@ -1478,14 +1479,33 @@ typedef struct {
PyObject *name;
PyObject *args;
PyObject *kwds;
PyObject **vectorcall_args; /* Borrowed references */
PyObject *vectorcall_kwnames;
vectorcallfunc vectorcall;
} methodcallerobject;

static PyObject *
methodcaller_vectorcall(
methodcallerobject *mc, PyObject *const *args, size_t nargsf, PyObject* kwnames)
{
if (!_PyArg_CheckPositional("methodcaller", PyVectorcall_NARGS(nargsf), 1, 1)
|| !_PyArg_NoKwnames("methodcaller", kwnames)) {
return NULL;
}
mc->vectorcall_args[0] = args[0];
return PyObject_VectorcallMethod(
mc->name, mc->vectorcall_args,
(1 + PyTuple_GET_SIZE(mc->args)) | PY_VECTORCALL_ARGUMENTS_OFFSET,
mc->vectorcall_kwnames);
}

/* AC 3.5: variable number of arguments, not currently support by AC */
static PyObject *
methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
methodcallerobject *mc;
PyObject *name;
PyObject *name, *key, *value;
Py_ssize_t nargs, i, ppos;

if (PyTuple_GET_SIZE(args) < 1) {
PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
Expand Down Expand Up @@ -1521,6 +1541,32 @@ methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}

nargs = PyTuple_GET_SIZE(args) - 1;
mc->vectorcall_args = PyMem_Calloc(
1 + nargs + (kwds ? PyDict_Size(kwds) : 0),
sizeof(PyObject *));
if (!mc->vectorcall_args) {
return PyErr_NoMemory();
}
/* The first item of vectorcall_args will be filled with obj. */
memcpy(mc->vectorcall_args + 1, PySequence_Fast_ITEMS(mc->args),
nargs * sizeof(PyObject *));
if (kwds) {
mc->vectorcall_kwnames = PySequence_Tuple(kwds);
if (!mc->vectorcall_kwnames) {
return NULL;
}
i = ppos = 0;
while (PyDict_Next(kwds, &ppos, &key, &value)) {
mc->vectorcall_args[1 + nargs + i] = value;
++i;
}
}
else {
mc->vectorcall_kwnames = NULL;
}
mc->vectorcall = (vectorcallfunc)methodcaller_vectorcall;

PyObject_GC_Track(mc);
return (PyObject *)mc;
}
Expand All @@ -1531,6 +1577,7 @@ methodcaller_clear(methodcallerobject *mc)
Py_CLEAR(mc->name);
Py_CLEAR(mc->args);
Py_CLEAR(mc->kwds);
Py_CLEAR(mc->vectorcall_kwnames);
return 0;
}

Expand All @@ -1540,6 +1587,7 @@ methodcaller_dealloc(methodcallerobject *mc)
PyTypeObject *tp = Py_TYPE(mc);
PyObject_GC_UnTrack(mc);
(void)methodcaller_clear(mc);
PyMem_Free(mc->vectorcall_args);
tp->tp_free(mc);
Py_DECREF(tp);
}
Expand Down Expand Up @@ -1696,6 +1744,12 @@ static PyMethodDef methodcaller_methods[] = {
reduce_doc},
{NULL}
};

static PyMemberDef methodcaller_members[] = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why is this being added?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It seems the logic has been introduced by #20026 instead of introducing a type slot for tp_vectorcall_offset. (Documentation is here)

{"__vectorcalloffset__", T_PYSSIZET, offsetof(methodcallerobject, vectorcall), READONLY},
{NULL}
};

PyDoc_STRVAR(methodcaller_doc,
"methodcaller(name, ...) --> methodcaller object\n\
\n\
Expand All @@ -1711,6 +1765,7 @@ static PyType_Slot methodcaller_type_slots[] = {
{Py_tp_traverse, methodcaller_traverse},
{Py_tp_clear, methodcaller_clear},
{Py_tp_methods, methodcaller_methods},
{Py_tp_members, methodcaller_members},
{Py_tp_new, methodcaller_new},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_repr, methodcaller_repr},
Expand All @@ -1722,7 +1777,7 @@ static PyType_Spec methodcaller_type_spec = {
.basicsize = sizeof(methodcallerobject),
.itemsize = 0,
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_IMMUTABLETYPE),
.slots = methodcaller_type_slots,
};

Expand Down

Back | FazBrowse Home | New Git URL