Skip to content

bpo-43687: use unicode_state empty string before unicode_init. without define WITH_DOC_STRINGS #25129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
19 changes: 16 additions & 3 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ extern "C" {
# define OVERALLOCATE_FACTOR 4
#endif


static int _Py_unicode_inited = 0;
static struct _Py_unicode_state*
get_unicode_state(void)
{
Expand All @@ -230,8 +230,19 @@ static inline PyObject* unicode_get_empty(void)
// Return a strong reference to the empty string singleton.
static inline PyObject* unicode_new_empty(void)
{
PyObject *empty = unicode_get_empty();
Py_INCREF(empty);
PyObject *empty = NULL;
if (_Py_unicode_inited == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change removes my micro-optimization:

static int
unicode_create_empty_string_singleton(struct _Py_unicode_state *state)
{
    // Use size=1 rather than size=0, so PyUnicode_New(0, maxchar) can be
    // optimized to always use state->empty_string without having to check if
    // it is NULL or not.

I wrote PR #25147 which reorganizes Py_Initialize() to fix https://bugs.python.org/issue43687 without losing the optimization.

empty = PyUnicode_New(1, 0);
if (empty == NULL) {
return NULL;
}
PyUnicode_1BYTE_DATA(empty)[0] = 0;
_PyUnicode_LENGTH(empty) = 0;
assert(_PyUnicode_CheckConsistency(empty, 1));
} else {
empty = unicode_get_empty();
Py_INCREF(empty);
}
return empty;
}

Expand Down Expand Up @@ -15713,6 +15724,7 @@ _PyUnicode_Init(PyInterpreterState *interp)
return _PyStatus_ERR("Can't initialize formatter iter type");
}
}
_Py_unicode_inited = 1;
return _PyStatus_OK();
}

Expand Down Expand Up @@ -16240,6 +16252,7 @@ _PyUnicode_Fini(PyInterpreterState *interp)
Py_CLEAR(state->latin1[i]);
}
Py_CLEAR(state->empty_string);
_Py_unicode_inited = 0;
}


Expand Down