Skip to content

Commit a195bce

Browse files
authored
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
1 parent d9ab95f commit a195bce

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up calls to ``bool()`` by using the :pep:`590` ``vectorcall`` calling
2+
convention. Patch by Dong-hee Na.

Objects/boolobject.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5555
return PyBool_FromLong(ok);
5656
}
5757

58+
static PyObject *
59+
bool_vectorcall(PyObject *type, PyObject * const*args,
60+
size_t nargsf, PyObject *kwnames)
61+
{
62+
long ok = 0;
63+
if (!_PyArg_NoKwnames("bool", kwnames)) {
64+
return NULL;
65+
}
66+
67+
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
68+
if (!_PyArg_CheckPositional("bool", nargs, 0, 1)) {
69+
return NULL;
70+
}
71+
72+
assert(PyType_Check(type));
73+
if (nargs) {
74+
ok = PyObject_IsTrue(args[0]);
75+
}
76+
if (ok < 0) {
77+
return NULL;
78+
}
79+
return PyBool_FromLong(ok);
80+
}
81+
5882
/* Arithmetic operations redefined to return bool if both args are bool. */
5983

6084
static PyObject *
@@ -170,6 +194,7 @@ PyTypeObject PyBool_Type = {
170194
0, /* tp_init */
171195
0, /* tp_alloc */
172196
bool_new, /* tp_new */
197+
.tp_vectorcall = bool_vectorcall,
173198
};
174199

175200
/* The objects representing bool values False and True */

0 commit comments

Comments
 (0)