bpo-45831: _Py_DumpASCII() uses a single write() call if possible (GH-29596) (GH-29597)

If the string is ASCII only and doesn't need to escape characters,
write the whole string with a single write() syscall.
(cherry picked from commit b919d8105c)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2021-11-17 13:59:42 -08:00 committed by GitHub
parent fd206b6807
commit ac89f8cab7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View file

@ -719,6 +719,26 @@ _Py_DumpASCII(int fd, PyObject *text)
truncated = 0;
}
// Is an ASCII string?
if (ascii->state.ascii) {
assert(kind == PyUnicode_1BYTE_KIND);
char *str = data;
int need_escape = 0;
for (i=0; i < size; i++) {
ch = str[i];
if (!(' ' <= ch && ch <= 126)) {
need_escape = 1;
break;
}
}
if (!need_escape) {
// The string can be written with a single write() syscall
_Py_write_noraise(fd, str, size);
goto done;
}
}
for (i=0; i < size; i++) {
if (kind != PyUnicode_WCHAR_KIND)
ch = PyUnicode_READ(kind, data, i);
@ -742,6 +762,8 @@ _Py_DumpASCII(int fd, PyObject *text)
_Py_DumpHexadecimal(fd, ch, 8);
}
}
done:
if (truncated) {
PUTS(fd, "...");
}