-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-105059: Remove anonymous union from PyObject #105275
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 |
---|---|---|
|
@@ -131,14 +131,14 @@ check by comparing the reference count field to the immortality reference count. | |
#define PyObject_HEAD_INIT(type) \ | ||
{ \ | ||
_PyObject_EXTRA_INIT \ | ||
{ _Py_IMMORTAL_REFCNT }, \ | ||
_Py_IMMORTAL_REFCNT, \ | ||
(type) \ | ||
}, | ||
#else | ||
#define PyObject_HEAD_INIT(type) \ | ||
{ \ | ||
_PyObject_EXTRA_INIT \ | ||
{ 1 }, \ | ||
1, \ | ||
(type) \ | ||
}, | ||
#endif /* Py_BUILD_CORE */ | ||
|
@@ -165,12 +165,7 @@ check by comparing the reference count field to the immortality reference count. | |
*/ | ||
struct _object { | ||
_PyObject_HEAD_EXTRA | ||
union { | ||
Py_ssize_t ob_refcnt; | ||
#if SIZEOF_VOID_P > 4 | ||
PY_UINT32_T ob_refcnt_split[2]; | ||
#endif | ||
}; | ||
Py_ssize_t ob_refcnt; | ||
PyTypeObject *ob_type; | ||
}; | ||
|
||
|
@@ -624,12 +619,12 @@ static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) | |
// directly PyObject.ob_refcnt. | ||
#if SIZEOF_VOID_P > 4 | ||
// Portable saturated add, branching on the carry flag and set low bits | ||
PY_UINT32_T cur_refcnt = op->ob_refcnt_split[PY_BIG_ENDIAN]; | ||
PY_UINT32_T cur_refcnt = _Py_CAST(PY_UINT32_T, op->ob_refcnt); | ||
PY_UINT32_T new_refcnt = cur_refcnt + 1; | ||
if (new_refcnt == 0) { | ||
return; | ||
} | ||
op->ob_refcnt_split[PY_BIG_ENDIAN] = new_refcnt; | ||
memcpy(&op->ob_refcnt, &new_refcnt, sizeof(new_refcnt)); | ||
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. Isn't this require the little-endian? 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. Maybe move the union here, use memcpy() and only use the bits that you need in the union? 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. The problem of the current implementation (without this change) is the usage of anonymous union in PyObject: it is not compatible with (strict) C99. A named union is fine with C89 and C99. It's more about finding the right syntax to get the most efficient code. |
||
#else | ||
// Explicitly check immortality against the immortal value | ||
if (_Py_IsImmortal(op)) { | ||
|
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.
It makes me sad to use memcpy() in the limited C API which should attempt to hide as many implementation details as possible. I don't get how the immortal object change interacts with the stable ABI. I would prefer to convert Py_INCREF/Py_DECREF to opaque function calls. It would avoid any C and C++ compatibility issue. At least, it would make the ABI a little bit more "forward compatible".
But that can be done later, since the priority is to unblock the next Python 3.12 beta release.
By the way, the nogil fork has also a completely different implementation which is also ABI incompatible unless these functions are implemented as opaque function calls.