Skip to content

Commit 415902a

Browse files
move interned dict under runtime state
1 parent cfaa79a commit 415902a

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

Include/internal/pycore_global_objects.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ struct _Py_global_objects {
4545
_PyGC_Head_UNUSED _tuple_empty_gc_not_used;
4646
PyTupleObject tuple_empty;
4747
} singletons;
48+
/* This dictionary holds all interned unicode strings. Note that references
49+
to strings in this dictionary are *not* counted in the string's ob_refcnt.
50+
When the interned string reaches a refcnt of 0 the string deallocation
51+
function will delete the reference from this dictionary.
52+
53+
Another way to look at this is that to say that the actual reference
54+
count of a string is: s->ob_refcnt + (s->state ? 2 : 0)
55+
*/
56+
PyObject *interned;
4857
};
4958

5059

Objects/unicodeobject.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6969
in _PyUnicode_ClearInterned(). */
7070
/* #define INTERNED_STATS 1 */
7171

72+
#define INTERNED_DICT() \
73+
_PyRuntime.global_objects.interned
7274

7375
/*[clinic input]
7476
class str "PyObject *" "&PyUnicode_Type"
@@ -191,16 +193,6 @@ extern "C" {
191193
# define OVERALLOCATE_FACTOR 4
192194
#endif
193195

194-
/* This dictionary holds all interned unicode strings. Note that references
195-
to strings in this dictionary are *not* counted in the string's ob_refcnt.
196-
When the interned string reaches a refcnt of 0 the string deallocation
197-
function will delete the reference from this dictionary.
198-
199-
Another way to look at this is that to say that the actual reference
200-
count of a string is: s->ob_refcnt + (s->state ? 2 : 0)
201-
*/
202-
static PyObject *interned = NULL;
203-
204196
/* Forward declaration */
205197
static inline int
206198
_PyUnicodeWriter_WriteCharInline(_PyUnicodeWriter *writer, Py_UCS4 ch);
@@ -1523,7 +1515,7 @@ unicode_dealloc(PyObject *unicode)
15231515
_Py_FatalRefcountError("deallocating an Unicode singleton");
15241516
}
15251517
#endif
1526-
1518+
PyObject *interned = INTERNED_DICT();
15271519
if (PyUnicode_CHECK_INTERNED(unicode)) {
15281520
/* Revive the dead object temporarily. PyDict_DelItem() removes two
15291521
references (key and value) which were ignored by
@@ -14657,12 +14649,14 @@ PyUnicode_InternInPlace(PyObject **p)
1465714649
return;
1465814650
}
1465914651

14652+
PyObject *interned = INTERNED_DICT();
1466014653
if (interned == NULL) {
1466114654
interned = PyDict_New();
1466214655
if (interned == NULL) {
1466314656
PyErr_Clear(); /* Don't leave an exception */
1466414657
return;
1466514658
}
14659+
INTERNED_DICT() = interned;
1466614660
}
1466714661

1466814662
PyObject *t = PyDict_SetDefault(interned, s, s);
@@ -14713,6 +14707,7 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
1471314707
return;
1471414708
}
1471514709

14710+
PyObject *interned = INTERNED_DICT();
1471614711
if (interned == NULL) {
1471714712
return;
1471814713
}
@@ -14748,7 +14743,7 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
1474814743
#endif
1474914744

1475014745
PyDict_Clear(interned);
14751-
Py_CLEAR(interned);
14746+
Py_CLEAR(INTERNED_DICT());
1475214747
}
1475314748

1475414749

@@ -15155,7 +15150,7 @@ _PyUnicode_EnableLegacyWindowsFSEncoding(void)
1515515150
static inline int
1515615151
unicode_is_finalizing(void)
1515715152
{
15158-
return (interned == NULL);
15153+
return (INTERNED_DICT() == NULL);
1515915154
}
1516015155
#endif
1516115156

@@ -15197,7 +15192,7 @@ _PyUnicode_Fini(PyInterpreterState *interp)
1519715192

1519815193
if (_Py_IsMainInterpreter(interp)) {
1519915194
// _PyUnicode_ClearInterned() must be called before _PyUnicode_Fini()
15200-
assert(interned == NULL);
15195+
assert(INTERNED_DICT() == NULL);
1520115196
// bpo-47182: force a unicodedata CAPI capsule re-import on
1520215197
// subsequent initialization of main interpreter.
1520315198
ucnhash_capi = NULL;

0 commit comments

Comments
 (0)