Repository navigation
gh-112075: Make PyDictKeysObject thread-safe - #114741
Conversation
fc6beda to
137eebe
Compare
| _Py_DecRefTotal(_PyInterpreterState_GET()); | ||
| #endif | ||
| if (--dk->dk_refcnt == 0) { | ||
| if (_Py_atomic_add_ssize(&dk->dk_refcnt, -1) == 1) { |
There was a problem hiding this comment.
How about adding a macro for the free-threading version and the default version?
There was a problem hiding this comment.
I did similar approach at listobject: 393cbef
See: _Py_SET_ITEMREF
There was a problem hiding this comment.
@corona10 is there a significant difference in performance between the two builds? I'm not sure if it is worth it to add these preprocessor guards everywhere if there is no measurable effect :) Is there a different motivation than performance?
There was a problem hiding this comment.
Regardless of whether we do the macro approach everywhere, I think it's a good idea in this specific case (specifically for performance).
137eebe to
0891529
Compare
0891529 to
398cb23
Compare
fe12c49 to
87c3446
Compare
| _Py_atomic_store_ssize(&keys->dk_nentries, keys->dk_nentries + 1); | ||
| _Py_atomic_store_ssize(&keys->dk_usable, keys->dk_usable - 1); |
There was a problem hiding this comment.
We can use a weaker ordering here that will be faster, especially on x86 where "release" doesn't require any memory barrier:
_Py_atomic_store_ssize_relaxed(&keys->dk_nentries, keys->dk_nentries + 1);
_Py_atomic_store_ssize_release(&keys->dk_usable, keys->dk_usable - 1);
(I don't think we have _Py_atomic_store_ssize_release yet, though)
I find the memory orderings hard to reason correctly about, so I like to model them with CDSChecker. Here's the model I used for this:
https://github.com/colesbury/c11-model-checker/blob/cpython-models/test/gh-112075.c
a22f723 to
808dd89
Compare
| if (_PyDict_HasSplitTable(mp)) { | ||
| LOCK_KEYS(keys); | ||
| dictkeys_incref(keys); | ||
| } else { |
There was a problem hiding this comment.
| } else { | |
| } | |
| else { |
808dd89 to
4914ba8
Compare
90bea6a to
88ab576
Compare
88ab576 to
a9d3666
Compare
Adds locking for shared keys
dictobjects thread-safe in--disable-gilbuilds #112075