8000 gh-112075: Iterating a dict shouldn't require locks by DinoV · Pull Request #115108 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112075: Iterating a di 8000 ct shouldn't require locks #115108

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
Feb 22, 2024
Merged
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
Prev Previous commit
acquire_key_value returns -1 and 0
  • Loading branch information
DinoV committed Feb 22, 2024
commit a1d7718c41d77640c2f9f69ce303195165ab41ff
20 changes: 10 additions & 10 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5274,15 +5274,15 @@ dictiter_iternextitem_lock_held(PyDictObject *d, PyObject *self,

// Grabs the key and/or value from the provided locations and if successful
// returns them with an increased reference count. If either one is unsucessful
// nothing is incref'd and returns 0.
// nothing is incref'd and returns -1.
static int
acquire_key_value(PyObject **key_loc, PyObject *value, PyObject **value_loc,
PyObject **out_key, PyObject **out_value)
{
if (out_key) {
*out_key = _Py_TryXGetRef(key_loc);
if (*out_key == NULL) {
return 0;
return -1;
}
}

Expand All @@ -5291,12 +5291,12 @@ acquire_key_value(PyObject **key_loc, PyObject *value, PyObject **value_loc,
if (out_key) {
Py_DECREF(*out_key);
}
return 0;
return -1;
}
*out_value = value;
}

return 1;
return 0;
}

static Py_ssize_t
Expand Down Expand Up @@ -5344,8 +5344,8 @@ dictiter_iternext_threadsafe(PyDictObject *d, PyObject *self,
// here.
int index = get_index_from_order(d, i);
PyObject *value = _Py_atomic_load_ptr(&values->values[index]);
if (!acquire_key_value(&DK_UNICODE_ENTRIES(k)[index].me_key, value,
&values->values[index], out_key, out_value)) {
if (acquire_key_value(&DK_UNICODE_ENTRIES(k)[index].me_key, value,
&values->values[index], out_key, out_value) < 0) {
goto try_locked;
}
}
Expand All @@ -5362,8 +5362,8 @@ dictiter_iternext_threadsafe(PyDictObject *d, PyObject *self,
if (i >= n)
goto fail;

if (!acquire_key_value(&entry_ptr->me_key, value,
&entry_ptr->me_value, out_key, out_value)) {
if (acquire_key_value(&entry_ptr->me_key, value,
&entry_ptr->me_value, out_key, out_value) < 0) {
goto try_locked;
}
}
Expand All @@ -5379,8 +5379,8 @@ dictiter_iternext_threadsafe(PyDictObject *d, PyObject *self,
if (i >= n)
goto fail;

if (!acquire_key_value(&entry_ptr->me_key, value,
&entry_ptr->me_value, out_key, out_value)) {
if (acquire_key_value(&entry_ptr->me_key, value,
&entry_ptr->me_value, out_key, out_value) < 0) {
goto try_locked;
}
}
Expand Down
0