Skip to content

gh-124642: Dictionaries aren't marking objects as weakref'd #124643

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
Sep 30, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed scalability issue in free-threaded builds for lock-free reads from dictionaries in multi-threaded scenarios
6 changes: 3 additions & 3 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ _Py_dict_lookup_threadsafe(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyOb
*value_addr = value;
if (value != NULL) {
assert(ix >= 0);
Py_INCREF(value);
_Py_NewRefWithLock(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a test for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not a great way to test this short of actually inspecting the ob_ref_shared field which isn't something we've done yet in free-threaded builds, so I'm not sure we want/need one.

The end result is just poor performance, but that's difficult to measure. It also requires a pretty particular set of circumstances to manifest, e.g. the shared ref count needs to already be 0 when we're increfing it here.

}
Py_END_CRITICAL_SECTION();
return ix;
Expand Down Expand Up @@ -7015,7 +7015,7 @@ _PyObject_TryGetInstanceAttribute(PyObject *obj, PyObject *name, PyObject **attr
// Still no dict, we can read from the values
assert(values->valid);
value = values->values[ix];
*attr = Py_XNewRef(value);
*attr = _Py_XNewRefWithLock(value);
success = true;
}

Expand All @@ -7035,7 +7035,7 @@ _PyObject_TryGetInstanceAttribute(PyObject *obj, PyObject *name, PyObject **attr

if (dict->ma_values == values && FT_ATOMIC_LOAD_UINT8(values->valid)) {
value = _Py_atomic_load_ptr_relaxed(&values->values[ix]);
*attr = Py_XNewRef(value);
*attr = _Py_XNewRefWithLock(value);
success = true;
} else {
// Caller needs to lookup from the dictionary
Expand Down
Loading