Commit 8697acc4 authored by Victor Stinner's avatar Victor Stinner

Optimize bytes.replace(b'', b'.')

Issue #26574: Optimize bytes.replace(b'', b'.') and
bytearray.replace(b'', b'.'): up to 80% faster. Patch written by Josh Snider.
parent f0896530
...@@ -339,6 +339,9 @@ Optimizations ...@@ -339,6 +339,9 @@ Optimizations
* Optimize :meth:`bytes.fromhex` and :meth:`bytearray.fromhex`: they are now * Optimize :meth:`bytes.fromhex` and :meth:`bytearray.fromhex`: they are now
between 2x and 3.5x faster. (Contributed by Victor Stinner in :issue:`25401`). between 2x and 3.5x faster. (Contributed by Victor Stinner in :issue:`25401`).
* Optimize ``bytes.replace(b'', b'.')`` and ``bytearray.replace(b'', b'.')``:
up to 80% faster. (Contributed by Josh Snider in :issue:`26574`).
Build and C API Changes Build and C API Changes
======================= =======================
......
...@@ -1376,6 +1376,7 @@ Mark Smith ...@@ -1376,6 +1376,7 @@ Mark Smith
Roy Smith Roy Smith
Ryan Smith-Roberts Ryan Smith-Roberts
Rafal Smotrzyk Rafal Smotrzyk
Josh Snider
Eric Snow Eric Snow
Dirk Soede Dirk Soede
Nir Soffer Nir Soffer
......
...@@ -10,6 +10,9 @@ Release date: tba ...@@ -10,6 +10,9 @@ Release date: tba
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #26574: Optimize ``bytes.replace(b'', b'.')`` and
``bytearray.replace(b'', b'.')``. Patch written by Josh Snider.
- Issue #26581: If coding cookie is specified multiple times on a line in - Issue #26581: If coding cookie is specified multiple times on a line in
Python source code file, only the first one is taken to account. Python source code file, only the first one is taken to account.
......
...@@ -1705,18 +1705,28 @@ replace_interleave(PyByteArrayObject *self, ...@@ -1705,18 +1705,28 @@ replace_interleave(PyByteArrayObject *self,
self_s = PyByteArray_AS_STRING(self); self_s = PyByteArray_AS_STRING(self);
result_s = PyByteArray_AS_STRING(result); result_s = PyByteArray_AS_STRING(result);
/* TODO: special case single character, which doesn't need memcpy */ if (to_len > 1) {
/* Lay the first one down (guaranteed this will occur) */ /* Lay the first one down (guaranteed this will occur) */
Py_MEMCPY(result_s, to_s, to_len); Py_MEMCPY(result_s, to_s, to_len);
result_s += to_len; result_s += to_len;
count -= 1; count -= 1;
for (i=0; i<count; i++) { for (i = 0; i < count; i++) {
*result_s++ = *self_s++; *result_s++ = *self_s++;
Py_MEMCPY(result_s, to_s, to_len); Py_MEMCPY(result_s, to_s, to_len);
result_s += to_len; result_s += to_len;
} }
}
else {
result_s[0] = to_s[0];
result_s += to_len;
count -= 1;
for (i = 0; i < count; i++) {
*result_s++ = *self_s++;
result_s[0] = to_s[0];
result_s += to_len;
}
}
/* Copy the rest of the original string */ /* Copy the rest of the original string */
Py_MEMCPY(result_s, self_s, self_len-i); Py_MEMCPY(result_s, self_s, self_len-i);
......
...@@ -2464,18 +2464,28 @@ replace_interleave(PyBytesObject *self, ...@@ -2464,18 +2464,28 @@ replace_interleave(PyBytesObject *self,
self_s = PyBytes_AS_STRING(self); self_s = PyBytes_AS_STRING(self);
result_s = PyBytes_AS_STRING(result); result_s = PyBytes_AS_STRING(result);
/* TODO: special case single character, which doesn't need memcpy */ if (to_len > 1) {
/* Lay the first one down (guaranteed this will occur) */ /* Lay the first one down (guaranteed this will occur) */
Py_MEMCPY(result_s, to_s, to_len); Py_MEMCPY(result_s, to_s, to_len);
result_s += to_len; result_s += to_len;
count -= 1; count -= 1;
for (i=0; i<count; i++) { for (i = 0; i < count; i++) {
*result_s++ = *self_s++; *result_s++ = *self_s++;
Py_MEMCPY(result_s, to_s, to_len); Py_MEMCPY(result_s, to_s, to_len);
result_s += to_len; result_s += to_len;
} }
}
else {
result_s[0] = to_s[0];
result_s += to_len;
count -= 1;
for (i = 0; i < count; i++) {
*result_s++ = *self_s++;
result_s[0] = to_s[0];
result_s += to_len;
}
}
/* Copy the rest of the original string */ /* Copy the rest of the original string */
Py_MEMCPY(result_s, self_s, self_len-i); Py_MEMCPY(result_s, self_s, self_len-i);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment