8000 bpo-40521: Empty frozenset is no longer a singleton by rhettinger · Pull Request #21085 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40521: Empty frozenset is no longer a singleton #21085

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 3 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Revert "bpo-40521: Make the empty frozenset per interpreter (GH-21068)"
This reverts commit 261cfed.
  • Loading branch information
rhettinger committed Jun 23, 2020
commit 00c029a6c22c7caadb17c6277b6f9c561210c51f
2 changes: 0 additions & 2 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ struct _is {
/* Using a cache is very effective since typically only a single slice is
created and then deleted again. */
PySliceObject *slice_cache;
// The empty frozenset is a singleton.
PyObject *empty_frozenset;

struct _Py_tuple_state tuple;
struct _Py_list_state list;
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ extern void _PyFrame_Fini(PyThreadState *tstate);
extern void _PyDict_Fini(PyThreadState *tstate);
extern void _PyTuple_Fini(PyThreadState *tstate);
extern void _PyList_Fini(PyThreadState *tstate);
extern void _PySet_Fini(PyThreadState *tstate);
extern void _PyBytes_Fini(PyThreadState *tstate);
extern void _PyFloat_Fini(PyThreadState *tstate);
extern void _PySlice_Fini(PyThreadState *tstate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ Each interpreter now its has own free lists, singletons and caches:

* Free lists: float, tuple, list, dict, frame, context,
asynchronous generator.
* Singletons: empty tuple, empty frozenset, empty bytes string,
* Singletons: empty tuple, empty bytes string,
single byte character.
* Slice cache.

They are no longer shared by all interpreters.

27 changes: 8 additions & 19 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,11 +975,12 @@ make_new_set_basetype(PyTypeObject *type, PyObject *iterable)
return make_new_set(type, iterable);
}

/* The empty frozenset is a singleton */
static PyObject *emptyfrozenset = NULL;

static PyObject *
make_new_frozenset(PyTypeObject *type, PyObject *iterable)
{
PyObject *res;

if (type != &PyFrozenSet_Type) {
return make_new_set(type, iterable);
}
Expand All @@ -990,7 +991,7 @@ make_new_frozenset(PyTypeObject *type, PyObject *iterable)
Py_INCREF(iterable);
return iterable;
}
res = make_new_set((PyTypeObject *)type, iterable);
PyObject *res = make_new_set((PyTypeObject *)type, iterable);
if (res == NULL || PySet_GET_SIZE(res) != 0) {
return res;
}
Expand All @@ -999,17 +1000,11 @@ make_new_frozenset(PyTypeObject *type, PyObject *iterable)
}

// The empty frozenset is a singleton
PyInterpreterState *interp = _PyInterpreterState_GET();
res = interp->empty_frozenset;
if (res == NULL) {
interp->empty_frozenset = make_new_set((PyTypeObject *)type, NULL);
res = interp->empty_frozenset;
if (res == NULL) {
return NULL;
}
if (emptyfrozenset == NULL) {
emptyfrozenset = make_new_set((PyTypeObject *)type, NULL);
}
Py_INCREF(res);
return res;
Py_XINCREF(emptyfrozenset);
return emptyfrozenset;
}

static PyObject *
Expand Down Expand Up @@ -2304,12 +2299,6 @@ PySet_Add(PyObject *anyset, PyObject *key)
return set_add_key((PySetObject *)anyset, key);
}

void
_PySet_Fini(PyThreadState *tstate)
{
Py_CLEAR(tstate->interp->empty_frozenset);
}

int
_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, Py_hash_t *hash)
{
Expand Down
4 changes: 3 additions & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,9 @@ finalize_interp_types(PyThreadState *tstate, int is_main_interp)
_PyAsyncGen_Fini(tstate);
_PyContext_Fini(tstate);

_PySet_Fini(tstate);
if (is_main_interp) {
_PySet_Fini();
}
_PyDict_Fini(tstate);
_PyList_Fini(tstate);
_PyTuple_Fini(tstate);
Expand Down
0