8000 gh-128137: Update PyASCIIObject to handle interned field with the ato… · python/cpython@ae23a01 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae23a01

Browse files
authored
gh-128137: Update PyASCIIObject to handle interned field with the atomic operation (gh-128196)
1 parent b60044b commit ae23a01

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

Include/cpython/unicodeobject.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ typedef struct {
109109
3: Interned, Immortal, and Static
110110
This categorization allows the runtime to determine the right
111111
cleanup mechanism at runtime shutdown. */
112-
unsigned int interned:2;
112+
uint16_t interned;
113113
/* Character size:
114114
115115
- PyUnicode_1BYTE_KIND (1):
@@ -132,21 +132,23 @@ typedef struct {
132132
* all characters are in the range U+0000-U+10FFFF
133133
* at least one character is in the range U+10000-U+10FFFF
134134
*/
135-
unsigned int kind:3;
135+
unsigned short kind:3;
136136
/* Compact is with respect to the allocation scheme. Compact unicode
137137
objects only require one memory block while non-compact objects use
138138
one block for the PyUnicodeObject struct and another for its data
139139
buffer. */
140-
unsigned int compact:1;
140+
unsigned short compact:1;
141141
/* The string only contains characters in the range U+0000-U+007F (ASCII)
142142
and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
143143
set, use the PyASCIIObject structure. */
144-
unsigned int ascii:1;
144+
unsigned short ascii:1;
145145
/* The object is statically allocated. */
146-
unsigned int statically_allocated:1;
146+
unsigned short statically_allocated:1;
147147
/* Padding to ensure that PyUnicode_DATA() is always aligned to
148-
4 bytes (see issue #19537 on m68k). */
149-
unsigned int :24;
148+
4 bytes (see issue #19537 on m68k) and we use unsigned short to avoid
149+
the extra four bytes on 32-bit Windows. This is restricted features
150+
for specific compilers including GCC, MSVC, Clang and IBM's XL compiler. */
151+
unsigned short :10;
150152
} state;
151153
} PyASCIIObject;
152154

@@ -195,7 +197,11 @@ typedef struct {
195197

196198
/* Use only if you know it's a string */
197199
static inline unsigned int PyUnicode_CHECK_INTERNED(PyObject *op) {
200+
#ifdef Py_GIL_DISABLED
201+
return _Py_atomic_load_uint16_relaxed(&_PyASCIIObject_CAST(op)->state.interned);
202+
#else
198203
return _PyASCIIObject_CAST(op)->state.interned;
204+
#endif
199205
}
200206
#define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
201207

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update :c:type:`PyASCIIObject` layout to handle interned field with the
2+
atomic operation. Patch by Donghee Na.

Objects/unicodeobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15729,7 +15729,7 @@ immortalize_interned(PyObject *s)
1572915729
_Py_DecRefTotal(_PyThreadState_GET());
1573015730
}
1573115731
#endif
15732-
_PyUnicode_STATE(s).interned = SSTATE_INTERNED_IMMORTAL;
15732+
FT_ATOMIC_STORE_UINT16_RELAXED(_PyUnicode_STATE(s).interned, SSTATE_INTERNED_IMMORTAL);
1573315733
_Py_SetImmortal(s);
1573415734
}
1573515735

@@ -15848,7 +15848,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
1584815848
_Py_DecRefTotal(_PyThreadState_GET());
1584915849
#endif
1585015850
}
15851-
_PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL;
15851+
FT_ATOMIC_STORE_UINT16_RELAXED(_PyUnicode_STATE(s).interned, SSTATE_INTERNED_MORTAL);
1585215852

1585315853
/* INTERNED_MORTAL -> INTERNED_IMMORTAL (if needed) */
1585415854

@@ -15984,7 +15984,7 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
1598415984
Py_UNREACHABLE();
1598515985
}
1598615986
if (!shared) {
15987-
_PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;
15987+
FT_ATOMIC_STORE_UINT16_RELAXED(_PyUnicode_STATE(s).interned, SSTATE_NOT_INTERNED);
1598815988
}
1598915989
}
1599015990
#ifdef INTERNED_STATS

0 commit comments

Comments
 (0)
0