Serhiy Storchaka added the comment:

I adapted a patch to the current code (now it works for bytearrays too), 
deleted the behavior change, left optimization only for 0- and 1-bytes 
separator, deleted tests (bytes.join tests already exists), wrote a benchmark 
and after long experiments has set a condition for this optimization. Now, I 
believe, the patch never leads to regression. The patch has been simplified, 
it's only 13 lines long.

Thank you, John O'Connor.

----------
Added file: http://bugs.python.org/file27621/bytes_join_optimization_2.patch
Added file: http://bugs.python.org/file27622/bytes_join_bench.sh

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue12805>
_______________________________________
diff -r 5ae4ff03b35f Objects/stringlib/join.h
--- a/Objects/stringlib/join.h  Thu Oct 18 22:18:42 2012 +0100
+++ b/Objects/stringlib/join.h  Fri Oct 19 14:05:51 2012 +0300
@@ -94,6 +94,19 @@
 
     /* Catenate everything. */
     p = STRINGLIB_STR(res);
+    if (!seplen || (seplen == 1 && nbufs > 2 && nbufs > (sz >> 5))) {
+        /* fast path */
+        if (seplen)
+            memset(p + buffers[0].len, sepstr[0],
+                   sz - buffers[0].len - buffers[nbufs - 1].len);
+        for (i = 0; i < nbufs; i++) {
+            Py_ssize_t n = buffers[i].len;
+            char *q = buffers[i].buf;
+            Py_MEMCPY(p, q, n);
+            p += n;
+        }
+        goto done;
+    }
     for (i = 0; i < nbufs; i++) {
         Py_ssize_t n;
         char *q;

Attachment: bytes_join_bench.sh
Description: application/shellscript

_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to