bpo-40313: speed up bytes.hex() (GH-19594)

Automerge-Triggered-By: @gpshead
This commit is contained in:
sweeneyde 2020-04-20 20:17:52 -04:00 committed by GitHub
parent bba760e9b6
commit 6a9e80a931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 12 deletions

View File

@ -0,0 +1 @@
Improve the performance of bytes.hex().

View File

@ -82,22 +82,59 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
/* Hexlify */
Py_ssize_t i, j;
unsigned char c;
if (bytes_per_sep_group == 0) {
for (i = j = 0; i < arglen; ++i) {
assert((j + 1) < resultlen);
unsigned char c;
c = (argbuf[i] >> 4) & 0x0f;
retbuf[j++] = Py_hexdigits[c];
c = argbuf[i] & 0x0f;
retbuf[j++] = Py_hexdigits[c];
if (bytes_per_sep_group && i < arglen - 1) {
Py_ssize_t anchor;
anchor = (bytes_per_sep_group > 0) ? (arglen - 1 - i) : (i + 1);
if (anchor % abs_bytes_per_sep == 0) {
retbuf[j++] = sep_char;
}
}
c = argbuf[i];
retbuf[j++] = Py_hexdigits[c >> 4];
retbuf[j++] = Py_hexdigits[c & 0x0f];
}
assert(j == resultlen);
}
else {
/* The number of complete chunk+sep periods */
Py_ssize_t chunks = (arglen - 1) / abs_bytes_per_sep;
Py_ssize_t chunk;
unsigned int k;
if (bytes_per_sep_group < 0) {
i = j = 0;
for (chunk = 0; chunk < chunks; chunk++) {
for (k = 0; k < abs_bytes_per_sep; k++) {
c = argbuf[i++];
retbuf[j++] = Py_hexdigits[c >> 4];
retbuf[j++] = Py_hexdigits[c & 0x0f];
}
retbuf[j++] = sep_char;
}
while (i < arglen) {
c = argbuf[i++];
retbuf[j++] = Py_hexdigits[c >> 4];
retbuf[j++] = Py_hexdigits[c & 0x0f];
}
assert(j == resultlen);
}
else {
i = arglen - 1;
j = resultlen - 1;
for (chunk = 0; chunk < chunks; chunk++) {
for (k = 0; k < abs_bytes_per_sep; k++) {
c = argbuf[i--];
retbuf[j--] = Py_hexdigits[c & 0x0f];
retbuf[j--] = Py_hexdigits[c >> 4];
}
retbuf[j--] = sep_char;
}
while (i >= 0) {
c = argbuf[i--];
retbuf[j--] = Py_hexdigits[c & 0x0f];
retbuf[j--] = Py_hexdigits[c >> 4];
}
assert(j == -1);
}
}
#ifdef Py_DEBUG
if (!return_bytes) {