Skip to content

bpo-36051: drop GIL during large b''.join operations #17757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,13 @@ def test_join(self):
self.assertEqual(dot_join([bytearray(b"ab"), b"cd"]), b"ab.:cd")
self.assertEqual(dot_join([b"ab", bytearray(b"cd")]), b"ab.:cd")
# Stress it with many items
seq = [b"abc"] * 1000
expected = b"abc" + b".:abc" * 999
seq = [b"abc"] * 100000
expected = b"abc" + b".:abc" * 99999
self.assertEqual(dot_join(seq), expected)
# Stress test with empty separator
seq = [b"abc"] * 100000
expected = b"abc" * 100000
self.assertEqual(self.type2test(b"").join(seq), expected)
self.assertRaises(TypeError, self.type2test(b" ").join, None)
# Error handling and cleanup when some item in the middle of the
# sequence has the wrong type.
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ Ezio Melotti
Doug Mennella
Dimitri Merejkowsky
Brian Merrell
Bruce Merry
Alexis Métaireau
Luke Mewburn
Carl Meyer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Drop the GIL during large ``bytes.join`` operations. Patch by Bruce Merry.
57 changes: 40 additions & 17 deletions Objects/stringlib/join.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
Py_buffer *buffers = NULL;
#define NB_STATIC_BUFFERS 10
Py_buffer static_buffers[NB_STATIC_BUFFERS];
#define GIL_THRESHOLD 1048576
int drop_gil = 1;
PyThreadState *save;

seq = PySequence_Fast(iterable, "can only join an iterable");
if (seq == NULL) {
Expand Down Expand Up @@ -65,12 +68,21 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
buffers[i].buf = PyBytes_AS_STRING(item);
buffers[i].len = PyBytes_GET_SIZE(item);
}
else if (PyObject_GetBuffer(item, &buffers[i], PyBUF_SIMPLE) != 0) {
PyErr_Format(PyExc_TypeError,
"sequence item %zd: expected a bytes-like object, "
"%.80s found",
i, Py_TYPE(item)->tp_name);
goto error;
else {
if (PyObject_GetBuffer(item, &buffers[i], PyBUF_SIMPLE) != 0) {
PyErr_Format(PyExc_TypeError,
"sequence item %zd: expected a bytes-like object, "
"%.80s found",
i, Py_TYPE(item)->tp_name);
goto error;
}
/* If the backing objects are mutable, then dropping the GIL
* opens up race conditions where another thread tries to modify
* the object which we hold a buffer on it. Such code has data
* races anyway, but this is a conservative approach that avoids
* changing the behaviour of that data race.
*/
drop_gil = 0;
}
nbufs = i + 1; /* for error cleanup */
itemlen = buffers[i].len;
Expand Down Expand Up @@ -102,6 +114,12 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)

/* Catenate everything. */
p = STRINGLIB_STR(res);
if (sz < GIL_THRESHOLD) {
drop_gil = 0; /* Benefits are likely outweighed by the overheads */
}
if (drop_gil) {
save = PyEval_SaveThread();
}
if (!seplen) {
/* fast path */
for (i = 0; i < nbufs; i++) {
Expand All @@ -110,19 +128,23 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
memcpy(p, q, n);
p += n;
}
goto done;
}
for (i = 0; i < nbufs; i++) {
Py_ssize_t n;
char *q;
if (i) {
memcpy(p, sepstr, seplen);
p += seplen;
else {
for (i = 0; i < nbufs; i++) {
Py_ssize_t n;
char *q;
if (i) {
memcpy(p, sepstr, seplen);
p += seplen;
}
n = buffers[i].len;
q = buffers[i].buf;
memcpy(p, q, n);
p += n;
}
n = buffers[i].len;
q = buffers[i].buf;
memcpy(p, q, n);
p += n;
}
if (drop_gil) {
PyEval_RestoreThread(save);
}
goto done;

Expand All @@ -138,3 +160,4 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
}

#undef NB_STATIC_BUFFERS
#undef GIL_THRESHOLD