8000 bpo-37207: Use PEP 590 vectorcall to speed up frozenset() by corona10 · Pull Request #19053 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37207: Use PEP 590 vectorcall to speed up frozenset() #19053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up calls to ``frozenset()`` by using the :pep:`590` ``vectorcall``
calling convention. Patch by Dong-hee Na.
70 changes: 51 additions & 19 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1063,36 +1063,67 @@ make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
/* The empty frozenset is a singleton */
static PyObject *emptyfrozenset = NULL;

static PyObject *
make_new_frozenset(PyTypeObject *type, PyObject *iterable)
{
assert(PyType_Check(type));
if (iterable != NULL) {
if (PyFrozenSet_CheckExact(iterable)) {
/* frozenset(f) is idempotent */
Py_INCREF(iterable);
return iterable;
}
PyObject *res = make_new_set((PyTypeObject *)type, iterable);
if (res == NULL || PySet_GET_SIZE(res) != 0) {
return res;
}
/* If the created frozenset is empty, return the empty frozenset singleton instead */
Py_DECREF(res);
}

// The empty frozenset is a singleton
if (emptyfrozenset == NULL) {
emptyfrozenset = make_new_set((PyTypeObject *)type, NULL);
}
Py_XINCREF(emptyfrozenset);
return emptyfrozenset;
}

static PyObject *
frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *iterable = NULL, *result;
PyObject *iterable = NULL;

if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset", kwds))
if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset", kwds)) {
return NULL;
}

if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable)) {
return NULL;
}

if (type != &PyFrozenSet_Type)
if (type != &PyFrozenSet_Type) {
return make_new_set(type, iterable);
}

if (iterable != NULL) {
/* frozenset(f) is idempotent */
if (PyFrozenSet_CheckExact(iterable)) {
Py_INCREF(iterable);
return iterable;
}
result = make_new_set(type, iterable);
if (result == NULL || PySet_GET_SIZE(result))
return result;
Py_DECREF(result);
return make_new_frozenset(type, iterable);
}

static PyObject *
frozenset_vectorcall(PyObject *type, PyObject * const*args,
size_t nargsf, PyObject *kwnames)
{
if (!_PyArg_NoKwnames("frozenset", kwnames)) {
return NULL;
}
/* The empty frozenset is a singleton */
if (emptyfrozenset == NULL)
emptyfrozenset = make_new_set(type, NULL);
Py_XINCREF(emptyfrozenset);
return emptyfrozenset;

Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (!_PyArg_CheckPositional("frozenset", nargs, 0, 1)) {
return NULL;
}

PyObject *iterable = (nargs ? args[0] : NULL);
return make_new_frozenset((PyTypeObject *)type, iterable);
}

static PyObject *
Expand Down Expand Up @@ -2283,6 +2314,7 @@ PyTypeObject PyFrozenSet_Type = {
PyType_GenericAlloc, /* tp_alloc */
frozenset_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = frozenset_vectorcall,
};


Expand Down
0