Skip to content

Commit

Permalink
bpo-40521: Make empty Unicode string per interpreter (pythonGH-21096)
Browse files Browse the repository at this point in the history
Each interpreter now has its own empty Unicode string singleton.
  • Loading branch information
vstinner authored and arun-mani-j committed Jul 21, 2020
1 parent 2372436 commit 6f46a1f
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 90 deletions.
2 changes: 2 additions & 0 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ struct _Py_bytes_state {
};

struct _Py_unicode_state {
// The empty Unicode object is a singleton to improve performance.
PyObject *empty;
struct _Py_unicode_fs_codec fs_codec;
};

Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc);

/* Various one-time initializers */

extern PyStatus _PyUnicode_Init(void);
extern PyStatus _PyUnicode_Init(PyThreadState *tstate);
extern int _PyStructSequence_Init(void);
extern int _PyLong_Init(PyThreadState *tstate);
extern PyStatus _PyFaulthandler_Init(int enable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Each interpreter now its has own free lists, singletons and caches:

* Free lists: float, tuple, list, dict, frame, context,
asynchronous generator, MemoryError.
* Singletons: empty tuple, empty bytes string,
* Singletons: empty tuple, empty bytes string, empty Unicode string,
single byte character.
* Slice cache.

Expand Down
1 change: 0 additions & 1 deletion Objects/stringlib/asciilib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define STRINGLIB_CHAR Py_UCS1
#define STRINGLIB_TYPE_NAME "unicode"
#define STRINGLIB_PARSE_CODE "U"
#define STRINGLIB_GET_EMPTY() unicode_empty
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
Expand Down
7 changes: 6 additions & 1 deletion Objects/stringlib/partition.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* stringlib: partition implementation */

#ifndef STRINGLIB_FASTSEARCH_H
#error must include "stringlib/fastsearch.h" before including this module
# error must include "stringlib/fastsearch.h" before including this module
#endif

#if !STRINGLIB_MUTABLE && !defined(STRINGLIB_GET_EMPTY)
# error "STRINGLIB_GET_EMPTY must be defined if STRINGLIB_MUTABLE is zero"
#endif


Py_LOCAL_INLINE(PyObject*)
STRINGLIB(partition)(PyObject* str_obj,
const STRINGLIB_CHAR* str, Py_ssize_t str_len,
Expand Down
4 changes: 0 additions & 4 deletions Objects/stringlib/stringdefs.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#ifndef STRINGLIB_STRINGDEFS_H
#define STRINGLIB_STRINGDEFS_H

#ifndef STRINGLIB_GET_EMPTY
# error "STRINGLIB_GET_EMPTY macro must be defined"
#endif

/* this is sort of a hack. there's at least one place (formatting
floats) where some stringlib code takes a different path if it's
compiled as unicode. */
Expand Down
1 change: 0 additions & 1 deletion Objects/stringlib/ucs1lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define STRINGLIB_CHAR Py_UCS1
#define STRINGLIB_TYPE_NAME "unicode"
#define STRINGLIB_PARSE_CODE "U"
#define STRINGLIB_GET_EMPTY() unicode_empty
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
Expand Down
1 change: 0 additions & 1 deletion Objects/stringlib/ucs2lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define STRINGLIB_CHAR Py_UCS2
#define STRINGLIB_TYPE_NAME "unicode"
#define STRINGLIB_PARSE_CODE "U"
#define STRINGLIB_GET_EMPTY() unicode_empty
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
Expand Down
1 change: 0 additions & 1 deletion Objects/stringlib/ucs4lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define STRINGLIB_CHAR Py_UCS4
#define STRINGLIB_TYPE_NAME "unicode"
#define STRINGLIB_PARSE_CODE "U"
#define STRINGLIB_GET_EMPTY() unicode_empty
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
Expand Down
1 change: 0 additions & 1 deletion Objects/stringlib/unicodedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#define STRINGLIB_CHAR Py_UNICODE
#define STRINGLIB_TYPE_NAME "unicode"
#define STRINGLIB_PARSE_CODE "U"
#define STRINGLIB_GET_EMPTY() unicode_empty
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
Expand Down
Loading

0 comments on commit 6f46a1f

Please sign in to comment.