gh-130312: SET_ADD should not lock (#130136)

SET_ADD should not lock
This commit is contained in:
Dino Viehland
2025-03-21 15:58:32 -07:00
committed by GitHub
parent 7101cba6bf
commit d9411ae3c2
5 changed files with 76 additions and 59 deletions

View File

@@ -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;
@@ -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;
@@ -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,