bpo-46944: use FASTCALL calling convention in generator.throw (GH-31723)

This commit is contained in:
Kumar Aditya 2022-03-11 20:07:14 +05:30 committed by GitHub
parent 4f74ffc5e3
commit 304197b382
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 14 deletions

View File

@ -0,0 +1 @@
Speed up throwing exception in generator with :const:`METH_FASTCALL` calling convention. Patch by Kumar Aditya.

View File

@ -561,16 +561,23 @@ failed_throw:
static PyObject * static PyObject *
gen_throw(PyGenObject *gen, PyObject *args) gen_throw(PyGenObject *gen, PyObject *const *args, Py_ssize_t nargs)
{ {
PyObject *typ; PyObject *typ;
PyObject *tb = NULL; PyObject *tb = NULL;
PyObject *val = NULL; PyObject *val = NULL;
if (!PyArg_UnpackTuple(args, "throw", 1, 3, &typ, &val, &tb)) { if (!_PyArg_CheckPositional("throw", nargs, 1, 3)) {
return NULL; 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); return _gen_throw(gen, 1, typ, val, tb);
} }
@ -813,7 +820,7 @@ PyDoc_STRVAR(sizeof__doc__,
static PyMethodDef gen_methods[] = { static PyMethodDef gen_methods[] = {
{"send",(PyCFunction)gen_send, METH_O, send_doc}, {"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}, {"close",(PyCFunction)gen_close, METH_NOARGS, close_doc},
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
@ -1159,7 +1166,7 @@ PyDoc_STRVAR(coro_close_doc,
static PyMethodDef coro_methods[] = { static PyMethodDef coro_methods[] = {
{"send",(PyCFunction)gen_send, METH_O, coro_send_doc}, {"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}, {"close",(PyCFunction)gen_close, METH_NOARGS, coro_close_doc},
{"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__}, {"__sizeof__", (PyCFunction)gen_sizeof, METH_NOARGS, sizeof__doc__},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
@ -1246,9 +1253,9 @@ coro_wrapper_send(PyCoroWrapper *cw, PyObject *arg)
} }
static PyObject * 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 * static PyObject *
@ -1266,7 +1273,8 @@ coro_wrapper_traverse(PyCoroWrapper *cw, visitproc visit, void *arg)
static PyMethodDef coro_wrapper_methods[] = { static PyMethodDef coro_wrapper_methods[] = {
{"send",(PyCFunction)coro_wrapper_send, METH_O, coro_send_doc}, {"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}, {"close",(PyCFunction)coro_wrapper_close, METH_NOARGS, coro_close_doc},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
@ -1789,7 +1797,7 @@ async_gen_asend_iternext(PyAsyncGenASend *o)
static PyObject * 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; PyObject *result;
@ -1800,7 +1808,7 @@ async_gen_asend_throw(PyAsyncGenASend *o, PyObject *args)
return NULL; 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); result = async_gen_unwrap_value(o->ags_gen, result);
if (result == NULL) { if (result == NULL) {
@ -1821,7 +1829,7 @@ async_gen_asend_close(PyAsyncGenASend *o, PyObject *args)
static PyMethodDef async_gen_asend_methods[] = { static PyMethodDef async_gen_asend_methods[] = {
{"send", (PyCFunction)async_gen_asend_send, METH_O, send_doc}, {"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}, {"close", (PyCFunction)async_gen_asend_close, METH_NOARGS, close_doc},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };
@ -2183,7 +2191,7 @@ check_error:
static PyObject * 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; PyObject *retval;
@ -2194,7 +2202,7 @@ async_gen_athrow_throw(PyAsyncGenAThrow *o, PyObject *args)
return NULL; return NULL;
} }
retval = gen_throw((PyGenObject*)o->agt_gen, args); retval = gen_throw((PyGenObject*)o->agt_gen, args, nargs);
if (o->agt_args) { if (o->agt_args) {
return async_gen_unwrap_value(o->agt_gen, retval); return async_gen_unwrap_value(o->agt_gen, retval);
} else { } else {
@ -2239,7 +2247,8 @@ async_gen_athrow_close(PyAsyncGenAThrow *o, PyObject *args)
static PyMethodDef async_gen_athrow_methods[] = { static PyMethodDef async_gen_athrow_methods[] = {
{"send", (PyCFunction)async_gen_athrow_send, METH_O, send_doc}, {"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}, {"close", (PyCFunction)async_gen_athrow_close, METH_NOARGS, close_doc},
{NULL, NULL} /* Sentinel */ {NULL, NULL} /* Sentinel */
}; };