-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-127065: Make methodcaller thread-safe and re-entrant #127245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
223d650
b6d454a
4ce1233
cf6b79b
2476ce4
5ecf876
709010d
6bd2c2e
8d40552
c9e3898
8ef7a04
b4f30d3
6d06201
56cdc1f
440eb0c
ad66951
bc3fe2a
e9a1fa6
00ab654
5a7344b
f9f53fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -1595,21 +1595,51 @@ static PyType_Spec attrgetter_type_spec = { | |||||||
| typedef struct { | ||||||||
| PyObject_HEAD | ||||||||
| PyObject *name; | ||||||||
| PyObject *xargs; // reference to arguments passed in constructor | ||||||||
| PyObject *args; | ||||||||
| PyObject *kwds; | ||||||||
| PyObject **vectorcall_args; /* Borrowed references */ | ||||||||
| PyObject *vectorcall_kwnames; | ||||||||
| vectorcallfunc vectorcall; | ||||||||
| } methodcallerobject; | ||||||||
|
|
||||||||
|
|
||||||||
| #define _METHODCALLER_MAX_ARGS 8 | ||||||||
|
|
||||||||
| static PyObject * | ||||||||
| methodcaller_vectorcall( | ||||||||
| methodcallerobject *mc, PyObject *const *args, size_t nargsf, PyObject* kwnames) | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the more common formatting looks like: methodcaller_vectorcall(methodcallerobject *mc, PyObject *const *args,
size_t nargsf, PyObject *kwnames) |
||||||||
| { | ||||||||
| printf("methodcaller_vectorcall\n"); | ||||||||
| if (!_PyArg_CheckPositional("methodcaller", PyVectorcall_NARGS(nargsf), 1, 1) | ||||||||
| || !_PyArg_NoKwnames("methodcaller", kwnames)) { | ||||||||
| return NULL; | ||||||||
| } | ||||||||
| assert(mc->vectorcall_args != NULL); | ||||||||
|
|
||||||||
| Py_ssize_t number_of_arguments = 1 + PyTuple_GET_SIZE(mc->args) + | ||||||||
| (mc->vectorcall_kwnames? PyTuple_GET_SIZE(mc->vectorcall_kwnames):0) + 1; | ||||||||
|
|
||||||||
| PyObject *tmp_args[_METHODCALLER_MAX_ARGS]; | ||||||||
| tmp_args[0] = args[0]; | ||||||||
| if (number_of_arguments) { | ||||||||
| assert(1 + number_of_arguments <= _METHODCALLER_MAX_ARGS); | ||||||||
| memcpy(tmp_args + 1, mc->vectorcall_args, sizeof(PyObject *) * number_of_arguments); | ||||||||
| } | ||||||||
| PyObject *result = PyObject_VectorcallMethod( | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Up to you, but I'd write this without the temporary |
||||||||
| mc->name, tmp_args, | ||||||||
| (1 + PyTuple_GET_SIZE(mc->args)) | PY_VECTORCALL_ARGUMENTS_OFFSET, | ||||||||
| mc->vectorcall_kwnames); | ||||||||
|
|
||||||||
| PyMem_Free(tmp_args); | ||||||||
| return result; | ||||||||
| } | ||||||||
|
|
||||||||
| static int _methodcaller_initialize_vectorcall(methodcallerobject* mc) | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| { | ||||||||
| PyObject* args = mc->xargs; | ||||||||
| PyObject* args = mc->args; | ||||||||
| PyObject* kwds = mc->kwds; | ||||||||
|
|
||||||||
| Py_ssize_t nargs = PyTuple_GET_SIZE(args); | ||||||||
| assert(nargs > 0); | ||||||||
| Py_ssize_t nargs = 1 + PyTuple_GET_SIZE(args); | ||||||||
| mc->vectorcall_args = PyMem_Calloc( | ||||||||
| nargs + (kwds ? PyDict_Size(kwds) : 0), | ||||||||
| sizeof(PyObject*)); | ||||||||
|
|
@@ -1619,8 +1649,8 @@ static int _methodcaller_initialize_vectorcall(methodcallerobject* mc) | |||||||
| } | ||||||||
| /* 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*)); | ||||||||
| memcpy(mc->vectorcall_args + 1, PySequence_Fast_ITEMS(args), | ||||||||
| (nargs - 1) * sizeof(PyObject*)); | ||||||||
| } | ||||||||
| if (kwds) { | ||||||||
| const Py_ssize_t nkwds = PyDict_Size(kwds); | ||||||||
|
|
@@ -1640,47 +1670,11 @@ static int _methodcaller_initialize_vectorcall(methodcallerobject* mc) | |||||||
| else { | ||||||||
| mc->vectorcall_kwnames = NULL; | ||||||||
| } | ||||||||
| return 1; | ||||||||
| } | ||||||||
| //mc->vectorcall = (vectorcallfunc)methodcaller_vectorcall; | ||||||||
|
|
||||||||
|
|
||||||||
| 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; | ||||||||
| } | ||||||||
| if (mc->vectorcall_args == NULL) { | ||||||||
| if (_methodcaller_initialize_vectorcall(mc) < 0) { | ||||||||
| return NULL; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| assert(mc->vectorcall_args != 0); | ||||||||
|
|
||||||||
| Py_ssize_t number_of_arguments = PyTuple_GET_SIZE(mc->xargs) + | ||||||||
| (mc->vectorcall_kwnames? PyTuple_GET_SIZE(mc->vectorcall_kwnames):0) + 1; | ||||||||
| size_t buffer_size = sizeof(PyObject *) * (number_of_arguments + 1); | ||||||||
|
|
||||||||
| // for number_of_arguments == 1 we could optimize by setting tmp_args equal to &args | ||||||||
| PyObject **tmp_args = (PyObject **) PyMem_Malloc(buffer_size); | ||||||||
| if (tmp_args == NULL) { | ||||||||
| PyErr_NoMemory(); | ||||||||
| return NULL; | ||||||||
| } | ||||||||
| memcpy(tmp_args, mc->vectorcall_args, buffer_size); | ||||||||
| tmp_args[0] = args[0]; | ||||||||
| return PyObject_VectorcallMethod( | ||||||||
| mc->name, tmp_args, | ||||||||
| (PyTuple_GET_SIZE(mc->xargs)) | PY_VECTORCALL_ARGUMENTS_OFFSET, | ||||||||
| mc->vectorcall_kwnames); | ||||||||
|
|
||||||||
| PyMem_Free(tmp_args); | ||||||||
| return 1; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
| /* AC 3.5: variable number of arguments, not currently support by AC */ | ||||||||
| static PyObject * | ||||||||
| methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||||||||
|
|
@@ -1713,11 +1707,27 @@ methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||||||
| _PyUnicode_InternMortal(interp, &name); | ||||||||
| mc->name = name; | ||||||||
|
|
||||||||
| mc->xargs = Py_XNewRef(args); // allows us to use borrowed references | ||||||||
| mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); | ||||||||
|
colesbury marked this conversation as resolved.
|
||||||||
| if (mc->args == NULL) { | ||||||||
| Py_DECREF(mc); | ||||||||
| return NULL; | ||||||||
| } | ||||||||
| mc->kwds = Py_XNewRef(kwds); | ||||||||
| mc->vectorcall_args = 0; | ||||||||
| mc->vectorcall_args = NULL; | ||||||||
|
|
||||||||
| Py_ssize_t vectorcall_size = PyTuple_GET_SIZE(args) | ||||||||
| + (kwds ? PyDict_Size(kwds) : 0); | ||||||||
| printf("methodcaller_new %d %d\n", PyTuple_GET_SIZE(args), vectorcall_size); | ||||||||
| if (vectorcall_size < (_METHODCALLER_MAX_ARGS-10)) { | ||||||||
| printf("hih\n"); | ||||||||
| if (_methodcaller_initialize_vectorcall(mc) < 0) { | ||||||||
| Py_XDECREF(mc->args); | ||||||||
| Py_XDECREF(mc->kwds); | ||||||||
| return NULL; | ||||||||
|
colesbury marked this conversation as resolved.
Outdated
|
||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| mc->vectorcall = (vectorcallfunc)methodcaller_vectorcall; | ||||||||
| //mc->vectorcall = (vectorcallfunc)methodcaller_vectorcall; | ||||||||
|
|
||||||||
| PyObject_GC_Track(mc); | ||||||||
| return (PyObject *)mc; | ||||||||
|
|
@@ -1726,19 +1736,22 @@ methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||||||
| static void | ||||||||
| methodcaller_clear(methodcallerobject *mc) | ||||||||
| { | ||||||||
| printf("methodcaller_clear\n"); | ||||||||
|
|
||||||||
| Py_CLEAR(mc->name); | ||||||||
| Py_CLEAR(mc->xargs); | ||||||||
| Py_CLEAR(mc->args); | ||||||||
| Py_CLEAR(mc->kwds); | ||||||||
| if (mc->vectorcall_args != NULL) { | ||||||||
| PyMem_Free(mc->vectorcall_args); | ||||||||
| mc->vectorcall_args = 0; | ||||||||
| Py_CLEAR(mc->vectorcall_kwnames); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| static void | ||||||||
| methodcaller_dealloc(methodcallerobject *mc) | ||||||||
| { | ||||||||
| printf("methodcaller_dealloc\n"); | ||||||||
|
|
||||||||
| PyTypeObject *tp = Py_TYPE(mc); | ||||||||
| PyObject_GC_UnTrack(mc); | ||||||||
| methodcaller_clear(mc); | ||||||||
|
|
@@ -1749,8 +1762,9 @@ methodcaller_dealloc(methodcallerobject *mc) | |||||||
| static int | ||||||||
| methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg) | ||||||||
| { | ||||||||
| printf("methodcaller_traverse\n"); | ||||||||
| Py_VISIT(mc->name); | ||||||||
| Py_VISIT(mc->xargs); | ||||||||
| Py_VISIT(mc->args); | ||||||||
| Py_VISIT(mc->kwds); | ||||||||
| Py_VISIT(Py_TYPE(mc)); | ||||||||
| return 0; | ||||||||
|
|
@@ -1759,6 +1773,8 @@ methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg) | |||||||
| static PyObject * | ||||||||
| methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw) | ||||||||
| { | ||||||||
| printf("methodcaller_call\n"); | ||||||||
| return Py_None; | ||||||||
| PyObject *method, *obj, *result; | ||||||||
|
|
||||||||
| if (!_PyArg_NoKeywords("methodcaller", kw)) | ||||||||
|
|
@@ -1770,15 +1786,7 @@ methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw) | |||||||
| if (method == NULL) | ||||||||
| return NULL; | ||||||||
|
|
||||||||
|
|
||||||||
| PyObject *cargs = PyTuple_GetSlice(mc->xargs, 1, PyTuple_GET_SIZE(mc->xargs)); | ||||||||
| if (cargs == NULL) { | ||||||||
| Py_DECREF(method); | ||||||||
| return NULL; | ||||||||
| } | ||||||||
|
|
||||||||
| result = PyObject_Call(method, cargs, mc->kwds); | ||||||||
| Py_DECREF(cargs); | ||||||||
| result = PyObject_Call(method, mc->args, mc->kwds); | ||||||||
| Py_DECREF(method); | ||||||||
| return result; | ||||||||
| } | ||||||||
|
|
@@ -1796,7 +1804,7 @@ methodcaller_repr(methodcallerobject *mc) | |||||||
| } | ||||||||
|
|
||||||||
| numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0; | ||||||||
| numposargs = PyTuple_GET_SIZE(mc->xargs) - 1; | ||||||||
| numposargs = PyTuple_GET_SIZE(mc->args); | ||||||||
| numtotalargs = numposargs + numkwdargs; | ||||||||
|
|
||||||||
| if (numtotalargs == 0) { | ||||||||
|
|
@@ -1812,7 +1820,7 @@ methodcaller_repr(methodcallerobject *mc) | |||||||
| } | ||||||||
|
|
||||||||
| for (i = 0; i < numposargs; ++i) { | ||||||||
| PyObject *onerepr = PyObject_Repr(PyTuple_GET_ITEM(mc->xargs, i+1)); | ||||||||
| PyObject *onerepr = PyObject_Repr(PyTuple_GET_ITEM(mc->args, i)); | ||||||||
| if (onerepr == NULL) | ||||||||
| goto done; | ||||||||
| PyTuple_SET_ITEM(argreprs, i, onerepr); | ||||||||
|
|
@@ -1864,14 +1872,14 @@ methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored)) | |||||||
| { | ||||||||
| if (!mc->kwds || PyDict_GET_SIZE(mc->kwds) == 0) { | ||||||||
| Py_ssize_t i; | ||||||||
| Py_ssize_t newarg_size = PyTuple_GET_SIZE(mc->xargs); | ||||||||
| PyObject *newargs = PyTuple_New(newarg_size); | ||||||||
| Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args); | ||||||||
| PyObject *newargs = PyTuple_New(1 + callargcount); | ||||||||
| if (newargs == NULL) | ||||||||
| return NULL; | ||||||||
| PyTuple_SET_ITEM(newargs, 0, Py_NewRef(mc->name)); | ||||||||
| for (i = 1; i < newarg_size; ++i) { | ||||||||
| PyObject *arg = PyTuple_GET_ITEM(mc->xargs, i); | ||||||||
| PyTuple_SET_ITEM(newargs, i, Py_NewRef(arg)); | ||||||||
| for (i = 0; i < callargcount; ++i) { | ||||||||
| PyObject *arg = PyTuple_GET_ITEM(mc->args, i); | ||||||||
| PyTuple_SET_ITEM(newargs, i + 1, Py_NewRef(arg)); | ||||||||
| } | ||||||||
| return Py_BuildValue("ON", Py_TYPE(mc), newargs); | ||||||||
| } | ||||||||
|
|
@@ -1889,12 +1897,7 @@ methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored)) | |||||||
| constructor = PyObject_VectorcallDict(partial, newargs, 2, mc->kwds); | ||||||||
|
|
||||||||
| Py_DECREF(partial); | ||||||||
| PyObject *args = PyTuple_GetSlice(mc->xargs, 1, PyTuple_GET_SIZE(mc->xargs)); | ||||||||
| if (!args) { | ||||||||
| Py_DECREF(constructor); | ||||||||
| return NULL; | ||||||||
| } | ||||||||
| return Py_BuildValue("NO", constructor, args); | ||||||||
| return Py_BuildValue("NO", constructor, mc->args); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ownership here seems a bit complicated and I think it can be simplified. As I understand it,
vectorcall_kwnamesonly exists to ensure that some entries invectorcall_argsstay alive.Instead, I'd suggest:
PyObject *vectorcall_argsa tuple (that holds strong references to its contents as usual)vectorcall_kwnames_PyTuple_ITEMSfor fast access to the contents of the tuple (formemcpy())vectorcall_argsinmethodcaller_traverseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
vectorcall_kwnamesis needed as an argument forPyObject_VectorcallMethodinmethodcaller_vectorcall(https://github.com/python/cpython/blob/main/Modules/_operator.c#L1666), so we cannot get rid of it.The ownership is not too hard I think: the objects in
vectorcall_argshave references borrowed from eithermc->argsor (the keys from)mc->kwds. I added a comment to clarify this.Making the
vectorcall_argsa tuple is still an option though. It requires a bit more memory and a tiny bit of computation in the initialization. It would be the C equivalent ofvectorcall_args = args + tuple(kwds). I'll work it out in a branch to see how it comparesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, in that case don't worry about it unless you prefer it as a tuple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff between the two approaches is this:
eendebakpt/cpython@methodcaller_ft...eendebakpt:cpython:methodcaller_ft_v2
What is nice about making
vectorcall_argsa tuple is that if there are no keyword arguments, we can reusemc->args. It does require more operations in the construction though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think either approach is fine! My guess is that the vast majority of uses of
methodcaller()do not use keyword arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running benchmarks shows what is to be expected from the implementations: using a tuple for
vectorcall_argsis a bit slower in initializing, except when there are no keyword arguments (since then we reuse theargtuple). Differences are small though.Since using a tuple leads to cleaner code and the majority of uses is without keywords I slightly prefer the tuple approach. I will open a new PR for it.