Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-128972: Add_Py_ALIGN_AS
and revertPyASCIIObject
memory layout.#133085
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
28551d5
9d4d011
95ffa4c
5b6685e
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -99,6 +99,11 @@ typedef struct { | ||
PyObject_HEAD | ||
Py_ssize_t length; /* Number of code points in the string */ | ||
Py_hash_t hash; /* Hash value; -1 if not set */ | ||
#ifdef Py_GIL_DISABLED | ||
/* Ensure 4 byte alignment for PyUnicode_DATA(), see gh-63736 on m68k. | ||
In the non-free-threaded build, we'll use explicit padding instead */ | ||
_Py_ALIGN_AS(4) | ||
Comment on lines +103 to +105 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. It does not ensure alignment for To ensure alignment for 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. Am I missing something? This sounds like a distinction without a difference.
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 alignment of The definition of the Perhaps 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. Huh? Why? 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. What exactly are you asking about? 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. Why? AFAIK, for FWIW, since 3.3 the header says "the data immediately follow the structure". We can't switch users to 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 data immediately following the structure (PyASCIIObject or PyCompactUnicodeObject) has the same alignment as the structure. Adding 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.
It does. Seehttps://gcc.godbolt.org/z/evh3hjxM8, for example. Arrays of structs (both 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. Hmm, it seems that the alignment of a structure is determined by the largest alignment of its members. I didn't find an explicit statement in the C11 standard, but it seems logical. If the structure has less alignment, then we can't guarantee the alignment of the members. So we can apply _Py_ALIGN_AS() to any member, and it will work independently of the compiler. The original issue (#63736) was because on some platforms 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. Yeah. | ||
#endif | ||
struct { | ||
/* If interned is non-zero, the two references from the | ||
dictionary to this object are *not* counted in ob_refcnt. | ||
@@ -109,7 +114,12 @@ typedef struct { | ||
3: Interned, Immortal, and Static | ||
This categorization allows the runtime to determine the right | ||
cleanup mechanism at runtime shutdown. */ | ||
#ifdef Py_GIL_DISABLED | ||
// Needs to be accessed atomically, so can't be a bit field. | ||
unsigned char interned; | ||
#else | ||
unsigned int interned:2; | ||
#endif | ||
/* Character size: | ||
- PyUnicode_1BYTE_KIND (1): | ||
@@ -132,23 +142,23 @@ typedef struct { | ||
* all characters are in the range U+0000-U+10FFFF | ||
* at least one character is in the range U+10000-U+10FFFF | ||
*/ | ||
unsignedint kind:3; | ||
/* Compact is with respect to the allocation scheme. Compact unicode | ||
objects only require one memory block while non-compact objects use | ||
one block for the PyUnicodeObject struct and another for its data | ||
buffer. */ | ||
unsignedint compact:1; | ||
/* The string only contains characters in the range U+0000-U+007F (ASCII) | ||
and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is | ||
set, use the PyASCIIObject structure. */ | ||
unsignedint ascii:1; | ||
/* The object is statically allocated. */ | ||
unsigned int statically_allocated:1; | ||
#ifndef Py_GIL_DISABLED | ||
/* Padding to ensure that PyUnicode_DATA() is always aligned to | ||
4 bytes (see issue gh-63736 on m68k) */ | ||
unsigned int :24; | ||
#endif | ||
} state; | ||
} PyASCIIObject; | ||
@@ -198,7 +208,7 @@ typedef struct { | ||
/* Use only if you know it's a string */ | ||
static inline unsigned int PyUnicode_CHECK_INTERNED(PyObject *op) { | ||
#ifdef Py_GIL_DISABLED | ||
return_Py_atomic_load_uint8_relaxed(&_PyASCIIObject_CAST(op)->state.interned); | ||
#else | ||
return _PyASCIIObject_CAST(op)->state.interned; | ||
#endif | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
For non-free-threaded builds, the memory layout of :c:struct:`PyASCIIObject` | ||
is reverted to match Python 3.13. (Note that the structure is not part of | ||
stable ABI and so its memory layout is *guaranteed* to remain stable.) |
Uh oh!
There was an error while loading.Please reload this page.