Visitar URL original
gh-100227: Move the Dict of Interned Strings to PyInterpreterState by ericsnowcurrently · Pull Request #102339 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ PyAPI_FUNC(void) _PyThreadState_Init(
PyThreadState *tstate);
PyAPI_FUNC(void) _PyThreadState_DeleteExcept(PyThreadState *tstate);

extern void _PyThreadState_InitDetached(PyThreadState *, PyInterpreterState *);
extern void _PyThreadState_ClearDetached(PyThreadState *);

extern PyObject * _Py_AddToGlobalDict(PyObject *, PyObject *, PyObject *);


static inline void
_PyThreadState_UpdateTracingState(PyThreadState *tstate)
Expand Down
3 changes: 0 additions & 3 deletions Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ extern PyTypeObject _PyExc_MemoryError;
.types = { \
.next_version_tag = 1, \
}, \
.cached_objects = { \
.main_tstate = _PyThreadState_INIT, \
}, \
.static_objects = { \
.singletons = { \
.small_ints = _Py_small_ints_INIT, \
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct _Py_unicode_runtime_ids {

struct _Py_unicode_runtime_state {
struct _Py_unicode_runtime_ids ids;
/* The interned dict is at _PyRuntime.cached_objects.interned_strings. */
};

/* fs_codec.encoding is initialized to NULL.
Expand Down
669 changes: 669 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Large diffs are not rendered by default.

57 changes: 29 additions & 28 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,16 @@ static inline PyObject *get_interned_dict(PyInterpreterState *interp)
return _Py_INTERP_CACHED_OBJECT(interp, interned_strings);
}

static PyObject *
ensure_interned_dict(PyInterpreterState *interp)
static int
init_interned_dict(PyInterpreterState *interp)
{
PyObject *interned = get_interned_dict(interp);
assert(get_interned_dict(interp) == NULL);
PyObject *interned = interned = PyDict_New();
if (interned == NULL) {
interned = PyDict_New();
if (interned == NULL) {
return NULL;
}
_Py_INTERP_CACHED_OBJECT(interp, interned_strings) = interned;
return -1;
}
return interned;
_Py_INTERP_CACHED_OBJECT(interp, interned_strings) = interned;
return 0;
}

static void
Expand Down Expand Up @@ -14551,21 +14549,29 @@ _PyUnicode_InitState(PyInterpreterState *interp)
PyStatus
_PyUnicode_InitGlobalObjects(PyInterpreterState *interp)
{
/* Intern statically allocated string identifiers and deepfreeze strings.
* This must be done before any module initialization so that statically
* allocated string identifiers are used instead of heap allocated strings.
* Deepfreeze uses the interned identifiers if present to save space
* else generates them and they are interned to speed up dict lookups.
*/
_PyUnicode_InitStaticStrings(interp);
// Initialize the global interned dict
if (init_interned_dict(interp)) {
PyErr_Clear();
return _PyStatus_ERR("failed to create interned dict");
}

if (_Py_IsMainInterpreter(interp)) {
/* Intern statically allocated string identifiers and deepfreeze strings.
* This must be done before any module initialization so that statically
* allocated string identifiers are used instead of heap allocated strings.
* Deepfreeze uses the interned identifiers if present to save space
* else generates them and they are interned to speed up dict lookups.
*/
_PyUnicode_InitStaticStrings(interp);

#ifdef Py_DEBUG
assert(_PyUnicode_CheckConsistency(&_Py_STR(empty), 1));
assert(_PyUnicode_CheckConsistency(&_Py_STR(empty), 1));

for (int i = 0; i < 256; i++) {
assert(_PyUnicode_CheckConsistency(LATIN1(i), 1));
}
for (int i = 0; i < 256; i++) {
assert(_PyUnicode_CheckConsistency(LATIN1(i), 1));
}
#endif
}

return _PyStatus_OK();
}
Expand Down Expand Up @@ -14617,11 +14623,8 @@ _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p)
return;
}

PyObject *interned = ensure_interned_dict(interp);
if (interned == NULL) {
PyErr_Clear(); /* Don't leave an exception */
return;
}
PyObject *interned = get_interned_dict(interp);
assert(interned != NULL);

PyObject *t = PyDict_SetDefault(interned, s, s);
if (t == NULL) {
Expand All @@ -14630,9 +14633,7 @@ _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p)
}

if (t != s) {
if (t != NULL) {
Py_SETREF(*p, Py_NewRef(t));
}
Py_SETREF(*p, Py_NewRef(t));
return;
}

Expand Down
4 changes: 0 additions & 4 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,6 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return status;
}

_PyThreadState_InitDetached(&runtime->cached_objects.main_tstate, interp);

*tstate_p = tstate;
return _PyStatus_OK();
}
Expand Down Expand Up @@ -1934,8 +1932,6 @@ Py_FinalizeEx(void)
// XXX Do this sooner during finalization.
// XXX Ensure finalizer errors are handled properly.

_PyThreadState_ClearDetached(&runtime->cached_objects.main_tstate);

finalize_interp_clear(tstate);
finalize_interp_delete(tstate->interp);

Expand Down
Loading