]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-37207: Use PEP 590 vectorcall to speed up set() constructor (GH-19019)
authorDong-hee Na <donghee.na92@gmail.com>
Mon, 16 Mar 2020 17:17:38 +0000 (02:17 +0900)
committerGitHub <noreply@github.com>
Mon, 16 Mar 2020 17:17:38 +0000 (18:17 +0100)
Misc/NEWS.d/next/Core and Builtins/2020-03-15-23-16-00.bpo-37207.6XbnQA.rst [new file with mode: 0644]
Objects/setobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-03-15-23-16-00.bpo-37207.6XbnQA.rst b/Misc/NEWS.d/next/Core and Builtins/2020-03-15-23-16-00.bpo-37207.6XbnQA.rst
new file mode 100644 (file)
index 0000000..28237cc
--- /dev/null
@@ -0,0 +1,2 @@
+Speed up calls to ``set()`` by using the :pep:`590` ``vectorcall`` calling
+convention. Patch by Dong-hee Na.
index 43fa5d17fd2e79be2469c257b46ddf53d7bfd6a0..9f424b3646140982450b177734bb445417c6e347 100644 (file)
@@ -2014,6 +2014,28 @@ set_init(PySetObject *self, PyObject *args, PyObject *kwds)
     return set_update_internal(self, iterable);
 }
 
+static PyObject*
+set_vectorcall(PyObject *type, PyObject * const*args,
+               size_t nargsf, PyObject *kwnames)
+{
+    assert(PyType_Check(type));
+
+    if (!_PyArg_NoKwnames("set", kwnames)) {
+        return NULL;
+    }
+
+    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+    if (!_PyArg_CheckPositional("set", nargs, 0, 1)) {
+        return NULL;
+    }
+
+    if (nargs) {
+        return make_new_set((PyTypeObject *)type, args[0]);
+    }
+
+    return make_new_set((PyTypeObject *)type, NULL);
+}
+
 static PySequenceMethods set_as_sequence = {
     set_len,                            /* sq_length */
     0,                                  /* sq_concat */
@@ -2162,6 +2184,7 @@ PyTypeObject PySet_Type = {
     PyType_GenericAlloc,                /* tp_alloc */
     set_new,                            /* tp_new */
     PyObject_GC_Del,                    /* tp_free */
+    .tp_vectorcall = set_vectorcall,
 };
 
 /* frozenset object ********************************************************/