[Python-checkins] bpo-42808: Add PyType_Type.tp_vectorcall for type(obj) performance (GH-24058)
corona10
webhook-mailer at python.org
Sun Feb 21 21:59:32 EST 2021
https://github.com/python/cpython/commit/b19855bb6ffd69a16e8b53873b19b0b04f488716
commit: b19855bb6ffd69a16e8b53873b19b0b04f488716
branch: master
author: Dennis Sweeney <36520290+sweeneyde at users.noreply.github.com>
committer: corona10 <donghee.na92 at gmail.com>
date: 2021-02-22T11:59:16+09:00
summary:
bpo-42808: Add PyType_Type.tp_vectorcall for type(obj) performance (GH-24058)
files:
A Misc/NEWS.d/next/Core and Builtins/2021-01-02-05-10-58.bpo-42808.AOxgxl.rst
M Objects/typeobject.c
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-01-02-05-10-58.bpo-42808.AOxgxl.rst b/Misc/NEWS.d/next/Core and Builtins/2021-01-02-05-10-58.bpo-42808.AOxgxl.rst
new file mode 100644
index 0000000000000..1c70005d0a09f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-01-02-05-10-58.bpo-42808.AOxgxl.rst
@@ -0,0 +1,2 @@
+Simple calls to ``type(object)`` are now faster due to the
+``vectorcall`` calling convention. Patch by Dennis Sweeney.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 33a7872ecc45c..8b115220630ad 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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".
@@ -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,
};
More information about the Python-checkins
mailing list