]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Optimize bytes.replace(b'', b'.')
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 21 Mar 2016 09:38:58 +0000 (10:38 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 21 Mar 2016 09:38:58 +0000 (10:38 +0100)
Issue #26574: Optimize bytes.replace(b'', b'.') and
bytearray.replace(b'', b'.'): up to 80% faster. Patch written by Josh Snider.

Doc/whatsnew/3.6.rst
Misc/ACKS
Misc/NEWS
Objects/bytearrayobject.c
Objects/bytesobject.c

index 904605861b7d2e19885df1a70c9c2c6eabbda44a..986c145dcc8f47371444aea19415afebfd175cb0 100644 (file)
@@ -339,6 +339,9 @@ Optimizations
 * 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
 =======================
index e67f6d1ef0d087288497b47496e2b8d30fcde3f0..52eae6944d4f60eed7dc46f00cbc3cadad46d0b6 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1376,6 +1376,7 @@ Mark Smith
 Roy Smith
 Ryan Smith-Roberts
 Rafal Smotrzyk
+Josh Snider
 Eric Snow
 Dirk Soede
 Nir Soffer
index 6f5c7ab0bf75549b95c435c7df6df8f10661241a..2fa82f348ec35b86d187a0f76941851ad5880fd5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: tba
 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.
 
index 9e8ba3999298d5b1a19bbfbb4f7a7eace2a5c2ce..209a64130e9f918b2b25ff4792192d45e7c5fb65 100644 (file)
@@ -1705,17 +1705,27 @@ replace_interleave(PyByteArrayObject *self,
     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<count; i++) {
-        *result_s++ = *self_s++;
+    if (to_len > 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 */
index 602dea681ce45d40d4b0aea3fb0101074a503b5e..5b9006eb9e29e20515bc7db7fc441ef6454af51b 100644 (file)
@@ -2464,17 +2464,27 @@ replace_interleave(PyBytesObject *self,
     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<count; i++) {
-        *result_s++ = *self_s++;
+    if (to_len > 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 */