bpo-42808: Add PyType_Type.tp_vectorcall for type(obj) performance (GH-24058)

This commit is contained in:
Dennis Sweeney 2021-02-21 21:59:16 -05:00 committed by GitHub
parent 01806d5beb
commit b19855bb6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Simple calls to ``type(object)`` are now faster due to the
``vectorcall`` calling convention. Patch by Dennis Sweeney.

View File

@ -2888,6 +2888,23 @@ error:
return NULL;
}
static PyObject *
type_vectorcall(PyObject *metatype, PyObject *const *args,
size_t nargsf, PyObject *kwnames)
{
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (nargs == 1 && metatype == (PyObject *)&PyType_Type){
if (!_PyArg_NoKwnames("type", kwnames)) {
return NULL;
}
return Py_NewRef(Py_TYPE(args[0]));
}
/* In other (much less common) cases, fall back to
more flexible calling conventions. */
PyThreadState *tstate = PyThreadState_GET();
return _PyObject_MakeTpCall(tstate, metatype, args, nargs, kwnames);
}
/* An array of type slot offsets corresponding to Py_tp_* constants,
* for use in e.g. PyType_Spec and PyType_GetSlot.
* Each entry has two offsets: "slot_offset" and "subslot_offset".
@ -3896,6 +3913,7 @@ PyTypeObject PyType_Type = {
type_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
(inquiry)type_is_gc, /* tp_is_gc */
.tp_vectorcall = type_vectorcall,
};