Skip to content

Commit fc3cd8e

Browse files
authored
gh-158585: Optimize bytes.join() for non-empty separator (#158689)
Remove the conditional branch from the loop.
1 parent b2bfbce commit fc3cd8e

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎Objects/stringlib/join.h‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
134134
}
135135
}
136136
else {
137-
for (i = 0; i < nbufs; i++) {
138-
Py_ssize_t n;
139-
char *q;
140-
if (i) {
141-
memcpy(p, sepstr, seplen);
142-
p += seplen;
143-
}
137+
Py_ssize_t n = buffers[0].len;
138+
char *q = buffers[0].buf;
139+
memcpy(p, q, n);
140+
p += n;
141+
142+
for (i = 1; i < nbufs; i++) {
143+
memcpy(p, sepstr, seplen);
144+
p += seplen;
145+
144146
n = buffers[i].len;
145147
q = buffers[i].buf;
146148
memcpy(p, q, n);

0 commit comments

Comments
 (0)