Skip to content
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

gh-130312: SET_ADD should not lock #130136

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions Include/internal/pycore_setobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ PyAPI_FUNC(int) _PySet_Contains(PySetObject *so, PyObject *key);
// Clears the set without acquiring locks. Used by _PyCode_Fini.
extern void _PySet_ClearInternal(PySetObject *so);

PyAPI_FUNC(int) _PySet_AddTakeRef(PySetObject *so, PyObject *key);



#ifdef __cplusplus
}
#endif
Expand Down
29 changes: 22 additions & 7 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ set_lookkey(PySetObject *so, PyObject *key, Py_hash_t hash)
static int set_table_resize(PySetObject *, Py_ssize_t);

static int
set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
set_add_entry_takeref(PySetObject *so, PyObject *key, Py_hash_t hash)
{
setentry *table;
setentry *freeslot;
Expand All @@ -133,12 +133,6 @@ set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
int probes;
int cmp;

_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);

/* Pre-increment is necessary to prevent arbitrary code in the rich
comparison from deallocating the key just before the insertion. */
Py_INCREF(key);

restart:

mask = so->mask;
Expand Down Expand Up @@ -209,6 +203,27 @@ set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
return -1;
}

static int
set_add_entry(PySetObject *so, PyObject *key, Py_hash_t hash)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(so);

return set_add_entry_takeref(so, Py_NewRef(key), hash);
}

int
_PySet_AddTakeRef(PySetObject *so, PyObject *key)
{
Py_hash_t hash = _PyObject_HashFast(key);
if (hash == -1) {
Py_DECREF(key);
return -1;
}
// We don't pre-increment here, the caller holds a strong
// reference to the object which we are stealing.
return set_add_entry_takeref(so, key, hash);
}

/*
Internal routine used by set_table_resize() to insert an item which is
known to be absent from the set. Besides the performance benefit,
Expand Down
22 changes: 14 additions & 8 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -964,9 +964,8 @@ dummy_func(
}

inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
int err = PySet_Add(PyStackRef_AsPyObjectBorrow(set),
PyStackRef_AsPyObjectBorrow(v));
PyStackRef_CLOSE(v);
int err = _PySet_AddTakeRef((PySetObject *)PyStackRef_AsPyObjectBorrow(set),
PyStackRef_AsPyObjectSteal(v));
ERROR_IF(err, error);
}

Expand Down Expand Up @@ -1899,22 +1898,29 @@ dummy_func(
}

inst(BUILD_SET, (values[oparg] -- set)) {
PyObject *set_o = PySet_New(NULL);
PySetObject *set_o = (PySetObject *)PySet_New(NULL);
if (set_o == NULL) {
DECREF_INPUTS();
ERROR_IF(true, error);
}

int err = 0;
for (int i = 0; i < oparg; i++) {
for (Py_ssize_t i = 0; i < oparg; i++) {
_PyStackRef value = values[i];
values[i] = PyStackRef_NULL;
if (err == 0) {
err = PySet_Add(set_o, PyStackRef_AsPyObjectBorrow(values[i]));
err = _PySet_AddTakeRef(set_o, PyStackRef_AsPyObjectSteal(value));
}
else {
PyStackRef_CLOSE(value);
}
}
DECREF_INPUTS();
if (err != 0) {
if (err) {
Py_DECREF(set_o);
ERROR_IF(true, error);
}

INPUTS_DEAD();
set = PyStackRef_FromPyObjectSteal(set_o);
}

Expand Down
44 changes: 21 additions & 23 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 20 additions & 24 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading