summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na92@gmail.com>2020-09-28 16:16:21 (GMT)
committerGitHub <noreply@github.com>2020-09-28 16:16:21 (GMT)
commita195bceff7b552c5f86dec7894ff24dcc87235da (patch)
tree9e8249575079f7f1041a939d5d4c66924877157e
parentd9ab95ff1fe81efdf70e545d536d9f6927d1ba81 (diff)
downloadcpython-a195bceff7b552c5f86dec7894ff24dcc87235da.zip
cpython-a195bceff7b552c5f86dec7894ff24dcc87235da.tar.gz
cpython-a195bceff7b552c5f86dec7894ff24dcc87235da.tar.bz2
bpo-41870: Use PEP 590 vectorcall to speed up bool() (GH-22427)
* bpo-41870: Use PEP 590 vectorcall to speed up bool() * bpo-41870: Add NEWS.d
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2020-09-27-22-23-14.bpo-41870.2v6_v4.rst2
-rw-r--r--Objects/boolobject.c25
2 files changed, 27 insertions, 0 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-09-27-22-23-14.bpo-41870.2v6_v4.rst b/Misc/NEWS.d/next/Core and Builtins/2020-09-27-22-23-14.bpo-41870.2v6_v4.rst
new file mode 100644
index 0000000..13a6bb0
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-09-27-22-23-14.bpo-41870.2v6_v4.rst
@@ -0,0 +1,2 @@
+Speed up calls to ``bool()`` by using the :pep:`590` ``vectorcall`` calling
+convention. Patch by Dong-hee Na.
diff --git a/Objects/boolobject.c b/Objects/boolobject.c
index 720835b..ab7669c 100644
--- a/Objects/boolobject.c
+++ b/Objects/boolobject.c
@@ -55,6 +55,30 @@ bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return PyBool_FromLong(ok);
}
+static PyObject *
+bool_vectorcall(PyObject *type, PyObject * const*args,
+ size_t nargsf, PyObject *kwnames)
+{
+ long ok = 0;
+ if (!_PyArg_NoKwnames("bool", kwnames)) {
+ return NULL;
+ }
+
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ if (!_PyArg_CheckPositional("bool", nargs, 0, 1)) {
+ return NULL;
+ }
+
+ assert(PyType_Check(type));
+ if (nargs) {
+ ok = PyObject_IsTrue(args[0]);
+ }
+ if (ok < 0) {
+ return NULL;
+ }
+ return PyBool_FromLong(ok);
+}
+
/* Arithmetic operations redefined to return bool if both args are bool. */
static PyObject *
@@ -170,6 +194,7 @@ PyTypeObject PyBool_Type = {
0, /* tp_init */
0, /* tp_alloc */
bool_new, /* tp_new */
+ .tp_vectorcall = bool_vectorcall,
};
/* The objects representing bool values False and True */