Add interning of unicode strings by copying the functionality from · python/cpython@1680713 · GitHub
Skip to content

Commit 1680713

Browse files
committed
Add interning of unicode strings by copying the functionality from
stringobject.c. Intern "True" and "False" in bool_repr() again as it was in the 8bit string era.
1 parent 34a042d commit 1680713

5 files changed

Lines changed: 158 additions & 7 deletions

File tree

Include/stringobject.h

Lines changed: 0 additions & 4 deletions

Include/unicodeobject.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,20 @@ typedef struct {
390390
Py_ssize_t length; /* Length of raw Unicode data in buffer */
391391
Py_UNICODE *str; /* Raw Unicode buffer */
392392
long hash; /* Hash value; -1 if not set */
393+
int state; /* != 0 if interned. In this case the two
394+
* references from the dictionary to this object
395+
* are *not* counted in ob_refcnt. */
393396
PyObject *defenc; /* (Default) Encoded version as Python
394397
string, or NULL; this is used for
395398
implementing the buffer protocol */
396399
} PyUnicodeObject;
397400

398401
PyAPI_DATA(PyTypeObject) PyUnicode_Type;
399402

403+
#define SSTATE_NOT_INTERNED 0
404+
#define SSTATE_INTERNED_MORTAL 1
405+
#define SSTATE_INTERNED_IMMORTAL 2
406+
400407
#define PyUnicode_Check(op) \
401408
PyType_FastSubclass((op)->ob_type, Py_TPFLAGS_UNICODE_SUBCLASS)
402409
#define PyUnicode_CheckExact(op) ((op)->ob_type == &PyUnicode_Type)
@@ -529,6 +536,14 @@ PyAPI_FUNC(PyObject*) PyUnicode_FromObject(
529536
PyAPI_FUNC(PyObject *) PyUnicode_FromFormatV(const char*, va_list);
530537
PyAPI_FUNC(PyObject *) PyUnicode_FromFormat(const char*, ...);
531538

539+
PyAPI_FUNC(void) PyUnicode_InternInPlace(PyObject **);
540+
PyAPI_FUNC(void) PyUnicode_InternImmortal(PyObject **);
541+
PyAPI_FUNC(PyObject *) PyUnicode_InternFromString(const char *);
542+
PyAPI_FUNC(void) _Py_ReleaseInternedUnicodeStrings(void);
543+
544+
/* Use only if you know it's a string */
545+
#define PyUnicode_CHECK_INTERNED(op) (((PyUnicodeObject *)(op))->state)
546+
532547
/* --- wchar_t support for platforms which support it --------------------- */
533548

534549
#ifdef HAVE_WCHAR_H

Modules/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,15 @@ Py_Main(int argc, char **argv)
521521
#ifdef __INSURE__
522522
/* Insure++ is a memory analysis tool that aids in discovering
523523
* memory leaks and other memory problems. On Python exit, the
524-
* interned string dictionary is flagged as being in use at exit
524+
* interned string dictionaries are flagged as being in use at exit
525525
* (which it is). Under normal circumstances, this is fine because
526526
* the memory will be automatically reclaimed by the system. Under
527527
* memory debugging, it's a huge source of useless noise, so we
528528
* trade off slower shutdown for less distraction in the memory
529529
* reports. -baw
530530
*/
531531
_Py_ReleaseInternedStrings();
532+
_Py_ReleaseInternedUnicodeStrings();
532533
#endif /* __INSURE__ */
533534

534535
return sts;

Objects/boolobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ bool_repr(PyObject *self)
2424

2525
if (self == Py_True)
2626
s = true_str ? true_str :
27-
(true_str = PyUnicode_FromString("True"));
27+
(true_str = PyUnicode_InternFromString("True"));
2828
else
2929
s = false_str ? false_str :
30-
(false_str = PyUnicode_FromString("False"));
30+
(false_str = PyUnicode_InternFromString("False"));
3131
Py_XINCREF(s);
3232
return s;
3333
}

Objects/unicodeobject.c

Lines changed: 139 additions & 0 deletions

0 commit comments

Comments
 (0)