Skip to content

Commit

Permalink
bpo-42808: Add PyType_Type.tp_vectorcall for type(obj) performance (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
sweeneyde authored Feb 22, 2021
1 parent 01806d5 commit b19855b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Simple calls to ``type(object)`` are now faster due to the
``vectorcall`` calling convention. Patch by Dennis Sweeney.
18 changes: 18 additions & 0 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2888,6 +2888,23 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
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".
Expand Down Expand Up @@ -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,
};


Expand Down

0 comments on commit b19855b

Please sign in to comment.