Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8b6b218

Browse filesBrowse files
corona10Seth Sims
authored and
Seth Sims
committed
bpo-41870: Use PEP 590 vectorcall to speed up bool() (pythonGH-22427)
* bpo-41870: Use PEP 590 vectorcall to speed up bool() * bpo-41870: Add NEWS.d
1 parent 2ec6e7a commit 8b6b218
Copy full SHA for 8b6b218

File tree

2 files changed

+27
-0
lines changed
Filter options

2 files changed

+27
-0
lines changed
+2Lines 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

Copy file name to clipboardExpand all lines: Objects/boolobject.c
+25Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.