Skip to content

bpo-46944: use FASTCALL calling convention in generator.throw #31723

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

Merged
merged 5 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speed up throwing exception in generator with :const:`METH_FASTCALL` calling convention. Patch by Kumar Aditya.
37 changes: 23 additions & 14 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,23 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,


static PyObject *
gen_throw(PyGenObject *gen, PyObject *args)
gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *typ;
PyObject *tb = NULL;
PyObject *val = NULL;

if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) {
if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) {
return NULL;
}

typ = args[0];
if (nargs == 3) {
val = args[1];
tb = args[2];
}
else if (nargs == 2) {
val = args[1];
}
return _gen_throw(gen, 1, typ, val, tb);
}

Expand Down Expand Up @@ -813,7 +820,7 @@ PyDoc_STRVAR(sizeof__doc__,

static PyMethodDef gen_methods[] = {
{"send",(PyCFunction)gen_send, METH_O, send_doc},
{"throw",(PyCFunction)gen_throw, METH_VARARGS, throw_doc},
{"throw",(PyCFunction)(void(*)(void))gen_throw, METH_FASTCALL, throw_doc},
{"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
{NULL, NULL} /* Sentinel */
Expand Down Expand Up @@ -1159,7 +1166,7 @@ PyDoc_STRVAR(coro_close_doc,

static PyMethodDef coro_methods[] = {
{"send",(PyCFunction)gen_send, METH_O, coro_send_doc},
{"throw",(PyCFunction)gen_throw, METH_VARARGS, coro_throw_doc},
{"throw",(PyCFunction)(void(*)(void))gen_throw, METH_FASTCALL, coro_throw_doc},
{"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
{NULL, NULL} /* Sentinel */
Expand Down Expand Up @@ -1246,9 +1253,9 @@ coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
}

static PyObject *
coro_wrapper_throw(PyCoroWrapper *cw, PyObject *args)
coro_wrapper_throw(PyCoroWrapper *cw, PyObject *const *args, Py_ssize_t nargs)
{
return gen_throw((PyGenObject *)cw->cw_coroutine, args);
return gen_throw((PyGenObject *)cw->cw_coroutine, args, nargs);
}

static PyObject *
Expand All @@ -1266,7 +1273,8 @@ coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)

static PyMethodDef coro_wrapper_methods[] = {
{"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc},
{"throw",(PyCFunction)coro_wrapper_throw, METH_VARARGS, coro_throw_doc},
{"throw",(PyCFunction)(void(*)(void))coro_wrapper_throw,
METH_FASTCALL, coro_throw_doc},
{"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
{NULL, NULL} /* Sentinel */
};
Expand Down Expand Up @@ -1789,7 +1797,7 @@ async_gen_asend_iternext(PyAsyncGenASend *o)


static PyObject *
async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args)
async_gen_asend_throw(PyAsyncGenASend *o, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *result;

Expand All @@ -1800,7 +1808,7 @@ async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args)
return NULL;
}

result = gen_throw((PyGenObject*)o->ags_gen, args);
result = gen_throw((PyGenObject*)o->ags_gen, args, nargs);
result = async_gen_unwrap_value(o->ags_gen, result);

if (result == NULL) {
Expand All @@ -1821,7 +1829,7 @@ async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)

static PyMethodDef async_gen_asend_methods[] = {
{"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc},
{"throw", (PyCFunction)async_gen_asend_throw, METH_VARARGS, throw_doc},
{"throw", (PyCFunction)(void(*)(void))async_gen_asend_throw, METH_FASTCALL, throw_doc},
{"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc},
{NULL, NULL} /* Sentinel */
};
Expand Down Expand Up @@ -2183,7 +2191,7 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)


static PyObject *
async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *retval;

Expand All @@ -2194,7 +2202,7 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
return NULL;
}

retval = gen_throw((PyGenObject*)o->agt_gen, args);
retval = gen_throw((PyGenObject*)o->agt_gen, args, nargs);
if (o->agt_args) {
return async_gen_unwrap_value(o->agt_gen, retval);
} else {
Expand Down Expand Up @@ -2239,7 +2247,8 @@ async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)

static PyMethodDef async_gen_athrow_methods[] = {
{"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc},
{"throw", (PyCFunction)async_gen_athrow_throw, METH_VARARGS, throw_doc},
{"throw", (PyCFunction)(void(*)(void))async_gen_athrow_throw,
METH_FASTCALL, throw_doc},
{"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc},
{NULL, NULL} /* Sentinel */
};
Expand Down