https://github.com/python/cpython/commit/fc3cd8e3f59fcae208bc2b53014dcb90fdc038cb
commit: fc3cd8e3f59fcae208bc2b53014dcb90fdc038cb
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-05T13:51:53+02:00
summary:

gh-158585: Optimize bytes.join() for non-empty separator (#158689)

Remove the conditional branch from the loop.

files:
M Objects/stringlib/join.h

diff --git a/Objects/stringlib/join.h b/Objects/stringlib/join.h
index fe460d41f6700f8..5fd2ca70f98dd31 100644
--- a/Objects/stringlib/join.h
+++ b/Objects/stringlib/join.h
@@ -134,13 +134,15 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
         }
     }
     else {
-        for (i = 0; i < nbufs; i++) {
-            Py_ssize_t n;
-            char *q;
-            if (i) {
-                memcpy(p, sepstr, seplen);
-                p += seplen;
-            }
+        Py_ssize_t n = buffers[0].len;
+        char *q = buffers[0].buf;
+        memcpy(p, q, n);
+        p += n;
+
+        for (i = 1; i < nbufs; i++) {
+            memcpy(p, sepstr, seplen);
+            p += seplen;
+
             n = buffers[i].len;
             q = buffers[i].buf;
             memcpy(p, q, n);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to