-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-39087: Optimize PyUnicode_AsUTF8AndSize(). #18327
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Optimize :c:func:`PyUnicode_AsUTF8` and :c:func:`PyUnicode_AsUTF8AndSize` | ||
slightly when they need to create internal UTF-8 cache. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3996,11 +3996,11 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr) | |||||
} | ||||||
|
||||||
|
||||||
static int unicode_fill_utf8(PyObject *unicode); | ||||||
|
||||||
const char * | ||||||
PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) | ||||||
{ | ||||||
PyObject *bytes; | ||||||
|
||||||
if (!PyUnicode_Check(unicode)) { | ||||||
PyErr_BadArgument(); | ||||||
return NULL; | ||||||
|
@@ -4009,21 +4009,9 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize) | |||||
return NULL; | ||||||
|
||||||
if (PyUnicode_UTF8(unicode) == NULL) { | ||||||
assert(!PyUnicode_IS_COMPACT_ASCII(unicode)); | ||||||
bytes = _PyUnicode_AsUTF8String(unicode, NULL); | ||||||
if (bytes == NULL) | ||||||
return NULL; | ||||||
_PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1); | ||||||
if (_PyUnicode_UTF8(unicode) == NULL) { | ||||||
PyErr_NoMemory(); | ||||||
Py_DECREF(bytes); | ||||||
if (unicode_fill_utf8(unicode) == -1) { | ||||||
return NULL; | ||||||
} | ||||||
_PyUnicode_UTF8_LENGTH(unicode) = PyBytes_GET_SIZE(bytes); | ||||||
memcpy(_PyUnicode_UTF8(unicode), | ||||||
PyBytes_AS_STRING(bytes), | ||||||
_PyUnicode_UTF8_LENGTH(unicode) + 1); | ||||||
Py_DECREF(bytes); | ||||||
} | ||||||
|
||||||
if (psize) | ||||||
|
@@ -5386,10 +5374,6 @@ static PyObject * | |||||
unicode_encode_utf8(PyObject *unicode, _Py_error_handler error_handler, | ||||||
const char *errors) | ||||||
{ | ||||||
enum PyUnicode_Kind kind; | ||||||
void *data; | ||||||
Py_ssize_t size; | ||||||
|
||||||
if (!PyUnicode_Check(unicode)) { | ||||||
PyErr_BadArgument(); | ||||||
return NULL; | ||||||
|
@@ -5402,22 +5386,86 @@ unicode_encode_utf8(PyObject *unicode, _Py_error_handler error_handler, | |||||
return PyBytes_FromStringAndSize(PyUnicode_UTF8(unicode), | ||||||
PyUnicode_UTF8_LENGTH(unicode)); | ||||||
|
||||||
kind = PyUnicode_KIND(unicode); | ||||||
data = PyUnicode_DATA(unicode); | ||||||
size = PyUnicode_GET_LENGTH(unicode); | ||||||
enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); | ||||||
void *data = PyUnicode_DATA(unicode); | ||||||
Py_ssize_t size = PyUnicode_GET_LENGTH(unicode); | ||||||
|
||||||
_PyBytesWriter writer; | ||||||
char *end; | ||||||
|
||||||
switch (kind) { | ||||||
default: | ||||||
Py_UNREACHABLE(); | ||||||
case PyUnicode_1BYTE_KIND: | ||||||
/* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ | ||||||
assert(!PyUnicode_IS_ASCII(unicode)); | ||||||
return ucs1lib_utf8_encoder(unicode, data, size, error_handler, errors); | ||||||
end = ucs1lib_utf8_encoder(&writer, unicode, data, size, error_handler, errors); | ||||||
break; | ||||||
case PyUnicode_2BYTE_KIND: | ||||||
end = ucs2lib_utf8_encoder(&writer, unicode, data, size, error_handler, errors); | ||||||
break; | ||||||
case PyUnicode_4BYTE_KIND: | ||||||
end = ucs4lib_utf8_encoder(&writer, unicode, data, size, error_handler, errors); | ||||||
break; | ||||||
} | ||||||
|
||||||
if (end == NULL) { | ||||||
_PyBytesWriter_Dealloc(&writer); | ||||||
return NULL; | ||||||
} | ||||||
return _PyBytesWriter_Finish(&writer, end); | ||||||
} | ||||||
|
||||||
static int | ||||||
unicode_fill_utf8(PyObject *unicode) | ||||||
{ | ||||||
/* the string cannot be ASCII, or PyUnicode_UTF8() would be set */ | ||||||
assert(!PyUnicode_IS_ASCII(unicode)); | ||||||
|
||||||
enum PyUnicode_Kind kind = PyUnicode_KIND(unicode); | ||||||
void *data = PyUnicode_DATA(unicode); | ||||||
Py_ssize_t size = PyUnicode_GET_LENGTH(unicode); | ||||||
|
||||||
_PyBytesWriter writer; | ||||||
char *end; | ||||||
|
||||||
switch (kind) { | ||||||
default: | ||||||
Py_UNREACHABLE(); | ||||||
case PyUnicode_1BYTE_KIND: | ||||||
end = ucs1lib_utf8_encoder(&writer, unicode, data, size, | ||||||
_Py_ERROR_STRICT, NULL); | ||||||
break; | ||||||
case PyUnicode_2BYTE_KIND: | ||||||
return ucs2lib_utf8_encoder(unicode, data, size, error_handler, errors); | ||||||
end = ucs2lib_utf8_encoder(&writer, unicode, data, size, | ||||||
_Py_ERROR_STRICT, NULL); | ||||||
break; | ||||||
case PyUnicode_4BYTE_KIND: | ||||||
return ucs4lib_utf8_encoder(unicode, data, size, error_handler, errors); | ||||||
end = ucs4lib_utf8_encoder(&writer, unicode, data, size, | ||||||
_Py_ERROR_STRICT, NULL); | ||||||
break; | ||||||
} | ||||||
if (end == NULL) { | ||||||
_PyBytesWriter_Dealloc(&writer); | ||||||
return -1; | ||||||
} | ||||||
|
||||||
char *start = writer.use_small_buffer ? writer.small_buffer : | ||||||
PyBytes_AS_STRING(writer.buffer); | ||||||
Py_ssize_t len = end - start; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: _PyBytesWriter_GetSize() exists, but using it would be slower and your code is correct ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is static function too. |
||||||
|
||||||
char *cache = PyObject_MALLOC(len + 1); | ||||||
if (cache == NULL) { | ||||||
_PyBytesWriter_Dealloc(&writer); | ||||||
PyErr_NoMemory(); | ||||||
return -1; | ||||||
} | ||||||
_PyUnicode_UTF8(unicode) = cache; | ||||||
_PyUnicode_UTF8_LENGTH(unicode) = len; | ||||||
memcpy(cache, start, len); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I read somewhere that memcpy(dst, src, 0) is an undefined behavior. Maybe:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, |
||||||
cache[len] = '\0'; | ||||||
_PyBytesWriter_Dealloc(&writer); | ||||||
return 0; | ||||||
} | ||||||
|
||||||
PyObject * | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use _PyBytesWriter_AsString() function here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is static function: we can not call it from here.