8000 bpo-42808: Add PyType_Type.tp_vectorcall for type(obj) performance (G… · python/cpython@b19855b · GitHub
[go: up one dir, main page]

Skip to content

Commit b19855b

Browse files
authored
bpo-42808: Add PyType_Type.tp_vectorcall for type(obj) performance (GH-24058)
1 parent 01806d5 commit b19855b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Simple calls to ``type(object)`` are now faster due to the
2+
``vectorcall`` calling convention. Patch by Dennis Sweeney.

Objects/typeobject.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2888,6 +2888,23 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
28882888
return NULL;
28892889
}
28902890

2891+
static PyObject *
2892+
type_vectorcall(PyObject *metatype, PyObject *const *args,
2893+
size_t nargsf, PyObject *kwnames)
2894+
{
2895+
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
2896+
if (nargs == 1 && metatype == (PyObject *)&PyType_Type){
2897+
if (!_PyArg_NoKwnames("type", kwnames)) {
2898+
return NULL;
2899+
}
2900+
return Py_NewRef(Py_TYPE(args[0]));
2901+
}
2902+
/* In other (much less common) cases, fall back to
2903+
more flexible calling conventions. */
2904+
PyThreadState *tstate = PyThreadState_GET();
2905+
return _PyObject_MakeTpCall(tstate, metatype, args, nargs, kwnames);
2906+
}
2907+
28912908
/* An array of type slot offsets corresponding to Py_tp_* constants,
28922909
* for use in e.g. PyType_Spec and PyType_GetSlot.
28932910
* Each entry has two offsets: "slot_offset" and "subslot_offset".
@@ -3896,6 +3913,7 @@ PyTypeObject PyType_Type = {
38963913
type_new, /* tp_new */
38973914
PyObject_GC_Del, /* tp_free */
38983915
(inquiry)type_is_gc, /* tp_is_gc */
3916+
.tp_vectorcall = type_vectorcall,
38993917
};
39003918

39013919

0 commit comments

Comments
 (0)
0