Skip to content

Commit

Permalink
bpo-41870: Use PEP 590 vectorcall to speed up bool() (pythonGH-22427)
Browse files Browse the repository at this point in the history
* bpo-41870: Use PEP 590 vectorcall to speed up bool()

* bpo-41870: Add NEWS.d
  • Loading branch information
corona10 authored and Seth Sims committed Oct 18, 2020
1 parent 2ec6e7a commit 8b6b218
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up calls to ``bool()`` by using the :pep:`590` ``vectorcall`` calling
convention. Patch by Dong-hee Na.
25 changes: 25 additions & 0 deletions Objects/boolobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 8b6b218

Please sign in to comment.