changeset: 100635:62e3b7af0697 user: Victor Stinner date: Mon Mar 21 10:38:58 2016 +0100 files: Doc/whatsnew/3.6.rst Misc/ACKS Misc/NEWS Objects/bytearrayobject.c Objects/bytesobject.c description: 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. diff -r ea9efa06c137 -r 62e3b7af0697 Doc/whatsnew/3.6.rst --- a/Doc/whatsnew/3.6.rst Fri Mar 18 21:52:22 2016 +0100 +++ b/Doc/whatsnew/3.6.rst Mon Mar 21 10:38:58 2016 +0100 @@ -339,6 +339,9 @@ * Optimize :meth:`bytes.fromhex` and :meth:`bytearray.fromhex`: they are now 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 ======================= diff -r ea9efa06c137 -r 62e3b7af0697 Misc/ACKS --- a/Misc/ACKS Fri Mar 18 21:52:22 2016 +0100 +++ b/Misc/ACKS Mon Mar 21 10:38:58 2016 +0100 @@ -1376,6 +1376,7 @@ Roy Smith Ryan Smith-Roberts Rafal Smotrzyk +Josh Snider Eric Snow Dirk Soede Nir Soffer diff -r ea9efa06c137 -r 62e3b7af0697 Misc/NEWS --- a/Misc/NEWS Fri Mar 18 21:52:22 2016 +0100 +++ b/Misc/NEWS Mon Mar 21 10:38:58 2016 +0100 @@ -10,6 +10,9 @@ 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 Python source code file, only the first one is taken to account. diff -r ea9efa06c137 -r 62e3b7af0697 Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Fri Mar 18 21:52:22 2016 +0100 +++ b/Objects/bytearrayobject.c Mon Mar 21 10:38:58 2016 +0100 @@ -1705,17 +1705,27 @@ self_s = PyByteArray_AS_STRING(self); result_s = PyByteArray_AS_STRING(result); - /* TODO: special case single character, which doesn't need memcpy */ - - /* Lay the first one down (guaranteed this will occur) */ - Py_MEMCPY(result_s, to_s, to_len); - result_s += to_len; - count -= 1; - - for (i=0; i 1) { + /* Lay the first one down (guaranteed this will occur) */ Py_MEMCPY(result_s, to_s, to_len); result_s += to_len; + count -= 1; + + for (i = 0; i < count; i++) { + *result_s++ = *self_s++; + Py_MEMCPY(result_s, to_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 */ diff -r ea9efa06c137 -r 62e3b7af0697 Objects/bytesobject.c --- a/Objects/bytesobject.c Fri Mar 18 21:52:22 2016 +0100 +++ b/Objects/bytesobject.c Mon Mar 21 10:38:58 2016 +0100 @@ -2464,17 +2464,27 @@ self_s = PyBytes_AS_STRING(self); result_s = PyBytes_AS_STRING(result); - /* TODO: special case single character, which doesn't need memcpy */ - - /* Lay the first one down (guaranteed this will occur) */ - Py_MEMCPY(result_s, to_s, to_len); - result_s += to_len; - count -= 1; - - for (i=0; i 1) { + /* Lay the first one down (guaranteed this will occur) */ Py_MEMCPY(result_s, to_s, to_len); result_s += to_len; + count -= 1; + + for (i = 0; i < count; i++) { + *result_s++ = *self_s++; + Py_MEMCPY(result_s, to_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 */