-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-135832: implement Py_DECREF specializations for Py_GIL_DISABLED build #135833
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,17 +272,93 @@ _Py_DECREF_NO_DEALLOC(PyObject *op) | |
} | ||
|
||
#else | ||
// TODO: implement Py_DECREF specializations for Py_GIL_DISABLED build | ||
|
||
static inline void | ||
_Py_DECREF_SPECIALIZED(PyObject *op, const destructor destruct) | ||
{ | ||
Py_DECREF(op); | ||
uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); | ||
if (local == _Py_IMMORTAL_REFCNT_LOCAL) { | ||
_Py_DECREF_IMMORTAL_STAT_INC(); | ||
return; | ||
} | ||
_Py_DECREF_STAT_INC(); | ||
#ifdef Py_REF_DEBUG | ||
_Py_DECREF_DecRefTotal(); | ||
#endif | ||
if (_Py_IsOwnedByCurrentThread(op)) { | ||
local--; | ||
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local); | ||
if (local == 0) { | ||
_Py_MergeZeroLocalRefcount(op); | ||
} | ||
} | ||
else { | ||
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); | ||
for (;;) { | ||
Py_ssize_t new_shared = shared - (1 << _Py_REF_SHARED_SHIFT); | ||
if (_Py_atomic_compare_exchange_ssize( | ||
Comment on lines
+298
to
+299
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is missing important parts of |
||
&op->ob_ref_shared, | ||
&shared, | ||
new_shared)) { | ||
if (new_shared == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can only safely deallocate if the reference count is zero and it's merged (i.e., new_shared is |
||
#ifdef Py_TRACE_REFS | ||
_Py_ForgetReference(op); | ||
#endif | ||
_PyReftracerTrack(op, PyRefTracer_DESTROY); | ||
destruct(op); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
static inline void | ||
_Py_DECREF_NO_DEALLOC(PyObject *op) | ||
Comment on lines
316
to
317
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this function is used anymore. |
||
{ | ||
Py_DECREF(op); | ||
uint32_t local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); | ||
if (local == _Py_IMMORTAL_REFCNT_LOCAL) { | ||
_Py_DECREF_IMMORTAL_STAT_INC(); | ||
return; | ||
} | ||
_Py_DECREF_STAT_INC(); | ||
#ifdef Py_REF_DEBUG | ||
_Py_DECREF_DecRefTotal(); | ||
#endif | ||
if (_Py_IsOwnedByCurrentThread(op)) { | ||
local--; | ||
_Py_atomic_store_uint32_relaxed(&op->ob_ref_local, local); | ||
if (local == 0) { | ||
_Py_MergeZeroLocalRefcount(op); | ||
} | ||
} | ||
else { | ||
Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); | ||
for (;;) { | ||
Py_ssize_t new_shared = shared - (1 << _Py_REF_SHARED_SHIFT); | ||
if (_Py_atomic_compare_exchange_ssize( | ||
&op->ob_ref_shared, | ||
&shared, | ||
new_shared)) { | ||
break; | ||
} | ||
} | ||
} | ||
#ifdef Py_DEBUG | ||
// Check that the refcount is still positive after decrement | ||
if (_Py_IsOwnedByCurrentThread(op)) { | ||
uint32_t final_local = _Py_atomic_load_uint32_relaxed(&op->ob_ref_local); | ||
if (final_local == 0) { | ||
_Py_FatalRefcountError("Expected a positive remaining refcount"); | ||
} | ||
} | ||
else { | ||
Py_ssize_t final_shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared); | ||
if ((final_shared >> _Py_REF_SHARED_SHIFT) <= 0) { | ||
_Py_FatalRefcountError("Expected a positive remaining refcount"); | ||
} | ||
} | ||
#endif | ||
} | ||
|
||
static inline int | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is barely used now, but there's a similar function
PyStackRef_CLOSE_SPECIALIZED
.There are two possible reasons to implement this as something that's not just
Py_DECREF
:_Py_Dealloc
so that you can enable this assertioncpython/Objects/object.c
Lines 3166 to 3170 in 6227662
_Py_MergeZeroLocalRefcount
because that internally may call_Py_Dealloc
I think (2) would be difficult to achieve because of
_Py_brc_queue_object
.