Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Store a thread state to use just for interned strings.
  • Loading branch information
ericsnowcurrently committed Mar 22, 2023
commit 5c20b84033b1a71a0e8db0245bfb6973f0482b32
5 changes: 5 additions & 0 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ struct _Py_unicode_runtime_ids {
};

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

Expand Down
30 changes: 30 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -14585,6 +14585,34 @@ _PyUnicode_InitTypes(PyInterpreterState *interp)
}


static PyThreadState *
get_interned_tstate(void)
{
PyThreadState *tstate = _PyRuntime.unicode_state.interned.tstate;
if (tstate == NULL) {
PyInterpreterState *main_interp = _PyInterpreterState_Main();
/* We do not "bind" the thread state here. */
tstate = _PyThreadState_New(main_interp);
if (tstate == NULL) {
PyErr_Clear();
return NULL;
}
_PyRuntime.unicode_state.interned.tstate = tstate;
}
return tstate;
}

static void
clear_interned_tstate(void)
{
PyThreadState *tstate = _PyRuntime.unicode_state.interned.tstate;
if (tstate != NULL) {
_PyRuntime.unicode_state.interned.tstate = NULL;
PyThreadState_Clear(tstate);
PyThreadState_Delete(tstate);
}
}

static inline PyObject *
store_interned(PyObject *obj)
{
Expand Down Expand Up @@ -14710,6 +14738,8 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
PyDict_Clear(interned);
Py_DECREF(interned);
set_interned_dict(NULL);

clear_interned_tstate();
}


Expand Down