-
-
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
Conversation
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 comment
The 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 comment
The 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 comment
The 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.
@@ -34,6 +33,7 @@ | |||
|
|||
#include <assert.h> // assert() | |||
#include <wchar.h> // wchar_t | |||
#include <string.h> // memcpy() |
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.
I'll be closing since there's better discussions on this in other places |
Currently, the use of an anonymous union causes warnings in C++ extensions. This updates the implementation of PyObject to not rely on anonymous unions and remove these warnings.
Current Issue that goes away after this patch:
Note: Currently, this is being fixed through the use of a memcpy but a couple of more options will be tried as well. I will also follow-up with benchmark numbers to verify we are not regressing.