Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lazy allocation of vectorcall arguments
  • Loading branch information
eendebakpt committed Jul 24, 2023
commit 486c498a43fc4e645a9fd62a19da07444181980c
90 changes: 60 additions & 30 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,52 @@ typedef struct {
vectorcallfunc vectorcall;
} methodcallerobject;

static void * _methodcaller_initialize_vectorcall(methodcallerobject* mc)
Comment thread
eendebakpt marked this conversation as resolved.
Outdated
{
PyObject* args = mc->xargs;
PyObject* kwds = mc->kwds;

Py_ssize_t nargs = PyTuple_GET_SIZE(args);
mc->vectorcall_args = PyMem_Calloc(
Comment thread
corona10 marked this conversation as resolved.
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 later */
if (nargs > 1) {
memcpy(mc->vectorcall_args, PySequence_Fast_ITEMS(args),
nargs * sizeof(PyObject*));
}
if (kwds) {
const Py_ssize_t nkwds = PyDict_Size(kwds);

mc->vectorcall_kwnames = PyTuple_New(nkwds);
if (!mc->vectorcall_kwnames) {
return NULL;
}
Py_ssize_t i = 0, ppos = 0;
PyObject* key, * value;
while (PyDict_Next(kwds, &ppos, &key, &value)) {
PyTuple_SET_ITEM(mc->vectorcall_kwnames, i, Py_NewRef(key));
mc->vectorcall_args[nargs + i] = value; // borrowed reference
++i;
}
}
else {
mc->vectorcall_kwnames = NULL;
}
return (void *)1;
}

static _methodcaller_clear_vectorcall(methodcallerobject* mc)
{
if (mc->vectorcall_args != NULL) {
PyMem_Free(mc->vectorcall_args);
Py_CLEAR(mc->vectorcall_kwnames);
}
}

static PyObject *
methodcaller_vectorcall(
methodcallerobject *mc, PyObject *const *args, size_t nargsf, PyObject* kwnames)
Expand All @@ -1564,19 +1610,28 @@ methodcaller_vectorcall(
|| !_PyArg_NoKwnames("methodcaller", kwnames)) {
return NULL;
}
if (mc->vectorcall_args == NULL) {
void *ret = _methodcaller_initialize_vectorcall(mc);
if (ret == NULL) {
return NULL;
}
Comment thread
eendebakpt marked this conversation as resolved.
Outdated
}

assert(mc->vectorcall_args != 0);
mc->vectorcall_args[0] = args[0];
return PyObject_VectorcallMethod(
mc->name, mc->vectorcall_args,
(PyTuple_GET_SIZE(mc->xargs)) | 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, *key, *value;
PyObject* name;
Comment thread
eendebakpt marked this conversation as resolved.
Outdated

if (PyTuple_GET_SIZE(args) < 1) {
PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
Expand Down Expand Up @@ -1604,36 +1659,9 @@ methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

mc->xargs = Py_XNewRef(args); // allows us to use borrowed references
mc->kwds = Py_XNewRef(kwds);
mc->vectorcall_args = 0;

Py_ssize_t nargs = PyTuple_GET_SIZE(args);
mc->vectorcall_args = PyMem_Calloc(
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 later */
if (nargs>1) {
memcpy(mc->vectorcall_args, PySequence_Fast_ITEMS(args),
nargs * sizeof(PyObject *));
}
if (kwds) {
const Py_ssize_t nkwds = PyDict_Size(kwds);

mc->vectorcall_kwnames = PyTuple_New(nkwds);
if (!mc->vectorcall_kwnames) {
return NULL;
}
Py_ssize_t i = 0, ppos = 0;
while (PyDict_Next(kwds, &ppos, &key, &value)) {
PyTuple_SET_ITEM(mc->vectorcall_kwnames, i, Py_NewRef(key));
mc->vectorcall_args[nargs + i] = value; // borrowed reference
++i;
}
}
else {
mc->vectorcall_kwnames = NULL;
}
mc->vectorcall = (vectorcallfunc)methodcaller_vectorcall;

PyObject_GC_Track(mc);
Expand All @@ -1646,7 +1674,9 @@ methodcaller_clear(methodcallerobject *mc)
Py_CLEAR(mc->name);
Py_CLEAR(mc->xargs);
Py_CLEAR(mc->kwds);
Py_CLEAR(mc->vectorcall_kwnames);

_methodcaller_clear_vectorcall(mc);
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.

Why did you separate the function?
Please, just inline the function in here if there is no specific reason.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To make it more symmetric with the _methodcaller_initialize_vectorcall. I will inline here. Let me know if the _methodcaller_initialize_vectorcall should also be inlined.


return 0;
}

Expand Down