Skip to content

Commit f363d0a

Browse files
authored
bpo-40521: Make empty Unicode string per interpreter (GH-21096)
Each interpreter now has its own empty Unicode string singleton.
1 parent d051801 commit f363d0a

File tree

12 files changed

+130
-90
lines changed

12 files changed

+130
-90
lines changed

Include/internal/pycore_interp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ struct _Py_bytes_state {
7171
};
7272

7373
struct _Py_unicode_state {
74+
// The empty Unicode object is a singleton to improve performance.
75+
PyObject *empty;
7476
struct _Py_unicode_fs_codec fs_codec;
7577
};
7678

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc);
3131

3232
/* Various one-time initializers */
3333

34-
extern PyStatus _PyUnicode_Init(void);
34+
extern PyStatus _PyUnicode_Init(PyThreadState *tstate);
3535
extern int _PyStructSequence_Init(void);
3636
extern int _PyLong_Init(PyThreadState *tstate);
3737
extern PyStatus _PyFaulthandler_Init(int enable);

Misc/NEWS.d/next/Core and Builtins/2020-05-20-01-17-34.bpo-40521.wvAehI.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Each interpreter now its has own free lists, singletons and caches:
22

33
* Free lists: float, tuple, list, dict, frame, context,
44
asynchronous generator, MemoryError.
5-
* Singletons: empty tuple, empty bytes string,
5+
* Singletons: empty tuple, empty bytes string, empty Unicode string,
66
single byte character.
77
* Slice cache.
88

Objects/stringlib/asciilib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define STRINGLIB_CHAR Py_UCS1
1212
#define STRINGLIB_TYPE_NAME "unicode"
1313
#define STRINGLIB_PARSE_CODE "U"
14-
#define STRINGLIB_GET_EMPTY() unicode_empty
1514
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
1615
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1716
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL

Objects/stringlib/partition.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
/* stringlib: partition implementation */
22

33
#ifndef STRINGLIB_FASTSEARCH_H
4-
#error must include "stringlib/fastsearch.h" before including this module
4+
# error must include "stringlib/fastsearch.h" before including this module
55
#endif
66

7+
#if !STRINGLIB_MUTABLE && !defined(STRINGLIB_GET_EMPTY)
8+
# error "STRINGLIB_GET_EMPTY must be defined if STRINGLIB_MUTABLE is zero"
9+
#endif
10+
11+
712
Py_LOCAL_INLINE(PyObject*)
813
STRINGLIB(partition)(PyObject* str_obj,
914
const STRINGLIB_CHAR* str, Py_ssize_t str_len,

Objects/stringlib/stringdefs.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#ifndef STRINGLIB_STRINGDEFS_H
22
#define STRINGLIB_STRINGDEFS_H
33

4-
#ifndef STRINGLIB_GET_EMPTY
5-
# error "STRINGLIB_GET_EMPTY macro must be defined"
6-
#endif
7-
84
/* this is sort of a hack. there's at least one place (formatting
95
floats) where some stringlib code takes a different path if it's
106
compiled as unicode. */

Objects/stringlib/ucs1lib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define STRINGLIB_CHAR Py_UCS1
1212
#define STRINGLIB_TYPE_NAME "unicode"
1313
#define STRINGLIB_PARSE_CODE "U"
14-
#define STRINGLIB_GET_EMPTY() unicode_empty
1514
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
1615
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1716
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL

Objects/stringlib/ucs2lib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define STRINGLIB_CHAR Py_UCS2
1212
#define STRINGLIB_TYPE_NAME "unicode"
1313
#define STRINGLIB_PARSE_CODE "U"
14-
#define STRINGLIB_GET_EMPTY() unicode_empty
1514
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
1615
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1716
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL

Objects/stringlib/ucs4lib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define STRINGLIB_CHAR Py_UCS4
1212
#define STRINGLIB_TYPE_NAME "unicode"
1313
#define STRINGLIB_PARSE_CODE "U"
14-
#define STRINGLIB_GET_EMPTY() unicode_empty
1514
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
1615
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1716
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL

Objects/stringlib/unicodedefs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#define STRINGLIB_CHAR Py_UNICODE
1414
#define STRINGLIB_TYPE_NAME "unicode"
1515
#define STRINGLIB_PARSE_CODE "U"
16-
#define STRINGLIB_GET_EMPTY() unicode_empty
1716
#define STRINGLIB_ISSPACE Py_UNICODE_ISSPACE
1817
#define STRINGLIB_ISLINEBREAK BLOOM_LINEBREAK
1918
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy